Mercurial hook precommit get repository path - python

I'm coding a script to check syntax of repository files.
So, I added to my .hgrc file this line :
precommit = python ~/tools/check_syntax.py
But I want get the path of repository in which I try to commit from check_syntax.py file.
Do you know a way to do that ?

hg root will give you the top of your current working directory. So something like:
[hooks]
precommit = python ~/tools/check_syntax.py $($HG root)
will set the top of your current working directory as first argument of your script.

Related

CircleCI - How to build and test in a sub directory?

I have an online course platform project. The tree looks like this:
app
-- edxapp
-- edx-platform
-- circle.yml
I want to run the circle.yml in the edx-platform directory. I've followed their documentation here.
First, I created a new circle.yml in the root directory so that the tree looks like this:
circle.yml
app
-- edxapp
-- edx-platform
-- circle.yml
The new circle.yml contains the following:
general:
build_dir: app/edxapp/edx-platform
But, it still didn't work. Then I tried another way. I linked the circle.yml files so that I have one circle.yml in each directory. Each circle.yml just contains the build_dir key with its value pointing to the next sub directory.
Please give me an explanation why this doesn't work. Also, please give me an alternative way to do it.
Note: The project structure has to be the same.
circle.yml needs to be in the root directory of a repository. That file in any other location will not be processed by CircleCI. Anything that you need to do (any commands, etc), needs to be done from that root file.
build-dir changes where the commands from circle.yml are run from, not where the file should be. In essence, whichever directory is set as build-dir, that becomes the working directory for commands in circle.yml. More details in CircleCI Docs.
In summary, have only 1 circle.yml directory, in the root of your repository. If you had a command ls, this would print the directory listing for ~/MY-REPO-NAME. If you set the build-dir to:
general:
build_dir: app/edxapp/edx-platform
then that same ls command will now print the directory listing of ~/MY-REPO-NAME/app/edxapp/edx-platform.
Regards,
Ricardo N Feliciano
Developer Evangelist, CircleCI

Run Python plugins in Hiero

I am facing a small problem in executing custom made python code from Hiero. As everybody knows, the first place to look for details is the offical documentation "Nukepedia", the Python Dev Guide and the Hiero User Guide. Well according to the Python Dev Guide, Hiero creates a ".hiero" directory by default which lets people add plugin paths to use them in the software. Well, I can't find that directory and I deduced after several tests that Hiero is using the init.py saved in the ".nuke" directory.
So I thought that maybe I could add the plugin paths there but kept getting the famous Import Error for the _fnpython module (before creating Python/Startup directory).
I added Python/Startup folders in .nuke and added the plugins in Startup, I got the same error, I even tried it by adding the path to the plugins in init.py and got the same error too.
Then I created a ".hiero" folder and did the same thing as before but Hiero never took that folder into consideration, I deduced that by printing some strings in the console, Hiero always took the init.py saved in the ".nuke" folder and kept showing the same error.
Finally, I tried to look into the installation process and try to seperate Nuke and Hiero's folders maybe that would create the ".hiero" directory but everything was automated.
The code that I want to run is given by Nuke and Hiero (in the examples directory), I just can't figure out what to do in order to run it from the program.
What should I do in order to fix this problem ?
Thank you in advance.
The setup for The Foundry HIERO is a little different than for NUKE.
HIERO has a core module. You'll see it in __init__.py file:
import FnRedirect
import FnPythonFixes
import core
import ui
try:
if hasattr(core, "taskRegistry"):
import importers
import exporters
except ImportError as e:
pass
I'm running HIERO and NUKE STUDIO on a Mac, so there's a full path to HIERO's __init__.py file inside package contents:
/Applications/Nuke10.5v5/Contents/MacOs/pythonextensions/site-packages/hiero/__init__.py
You need to import this module using:
import hiero.core
or using a reference to the core package:
from core import *
To find HIERO's current paths you have to run this line in its Script Editor:
print hiero.core.pluginPath()
Click this link for further details: Hiero's Environment Setup
All these instructions are suitable for macOS 10.9 and above. Here are two blocks of steps: first for Terminal Mode and second for UI Mode.
BLOCK 1: setup for Terminal Sessions
You need to manually create .hiero directory in your Home area.
The recommended default location for running Python on startup is:
~/.hiero/Python/Startup
~/.hiero/Python/StartupUI
Type in your bash Terminal (when you're inside your Home user directory) the following line:
mkdir .hiero/
then:
mkdir .hiero/Python/
and then:
mkdir .hiero/Python/StartupUI/
then navigate to Home directory with:
cd ~
and check it with:
ls -a
Also you can specify any number of user-defined paths using the environment variable HIERO_PLUGIN_PATH, just like the standard Unix PATH environment variable.
For that you need to set up an environment variable in .bash_profile. To run in Terminal PICO editor just type (remember you need an administrator's password for sudo command):
sudo pico .bash_profile
and paste these three lines in it (change swift for <yourName> and save this file):
echo HIERO environment var is set...
export HIERO_PLUGIN_PATH=/Users/swift/.hiero/Python/StartupUI/
export PATH=$PATH:$HIERO_PLUGIN_PATH
Write out a file with ctrl o
Exit pico editor with ctrl x
Restart Terminal
In Terminal you could print this environment variable typing:
printenv HIERO_PLUGIN_PATH
You should put inside that StartupUI directory menu.py, any set of xxxx.py or xxxx.pyc files, as well as __init__.py file.
Now you can use /Users/swift/.hiero/Python/StartupUI/ path in Terminal Mode.
BLOCK 2: setup for UI Sessions
To assign the current paths that HIERO searches when loading plug-ins, you need to create __init__.py file with the following lines:
import hiero.core
path='/Users/swift/.hiero/Python/Startup/'
hiero.core.addPluginPath(path)
After that make Python/Startup/ subdirectories under ~/.nuke/ folder.
It's not a mistake: I typed .nuke.
Then place this __init__.py file into /Users/swift/.nuke/Python/Startup/ directory.
Restart HIERO (or NUKE STUDIO) if it works.
After that launch HIERO or NUKE STUDIO and run
print hiero.core.pluginPath()
command in the HIERO's Script Editor or in NUKE STUDIO's Script Editor and you'll see this result:
After that you'll find new __init__.pyc file in /Users/swift/.nuke/Python/Startup/ directory.
Each package or module discovered when you launch HIERO is imported and added to the built-in package hiero.plugins.
TEST 1: custom_guides.py
I do not have a commercial version of HIERO so I tested custom_guides.py script ( found here ) using NUKE STUDIO NC.
I placed custom_guides.py in ~/.nuke/Python/Startup directory and then added two lines to NUKE's init.py file located in ~/.nuke directory.
import nuke
nuke.pluginAddPath("./Python/Startup")
The only thing I could say: "it works" Do the same actions as I did and it'll work for HIERO.
Look at safe_zone and masking_ratio dropdown menus. They are different: before and after.
Before uploading custom_guides.py script:
After uploading custom_guides.py script:
# file custom_guides.py contains these lines:
viewer_masks = [
hiero.ui.guides.MaskGuide("NTSC", 0.91),
hiero.ui.guides.MaskGuide("PAL", 1.09),
hiero.ui.guides.MaskGuide("NTSC_16:9", 1.21),
hiero.ui.guides.MaskGuide("PAL_16:9", 1.46),
hiero.ui.guides.MaskGuide("Cinemascope 2:1", 2.0)
]
TEST 2: web_browser.py
I placed web_browser.py file in ~/.nuke/Python/Startup directory. This Python script creates dockable panel with web browser written with PySide Qt.
I do not have a commercial version of HIERO so I tested web_browser.py script ( found here ) using NUKE STUDIO NC.

Python git module Invalid git repository error

I have a script that is placed in a folder structure as such:
~/wofc/folder1/folder2/script.py
script.py uses the git module to go some tasks. However when I run the script from outside of folder2 i.e. when I have cd into folder1 i run python folder2/script.py arg1 arg2 I get the raise InvalidGitRepositoryError(epath) error. The script runs fine when I run it from inside folder2 i.e. cd into folder2 and run python script.py arg1 arg2. Below is the relevant code snippet. Can You please let me know what is the issue?
git = Repo('{}/..'.format(os.getcwd())).git
git.checkout('master')
git.pull()
Instead of Repo('{}/..'.format(os.getcwd())).git, use os.path.abspath:
git = Repo(os.path.abspath('{}/..'.format(os.getcwd())).git
git.checkout('master')
git.pull()
To run git commands, the current folder should be a git repo.
.git repo should be present to execute git commands.
That is the reason for the error.
The problem is that you use os.getcwd() which returns the current working directory. If you stand just outside folder2 this function will return ~/wofc/folder1.
You should swap it for something like:
import os
os.path.dirname(os.path.abspath(__file__))
For example like this:
import os
path = os.path.dirname(os.path.abspath(__file__))
git = Repo('{}/..'.format(path)).git
git.checkout('master')
git.pull()
As user1846747 said, gitPython requires a Repo object to run a git command.
This is a classic bootstrap issue (chicken and egg problem): "how can I run a git command running gitPython to find where the Repo root is, when I need to know where the root is to create a Repo object to run a git command?"
#MaxNoe solved this in Find the root of the git repository where the file lives with his python-gitpath project httpsgithub.com/MaxNoe/python-gitpath

What do I specify for "Rope project root folder: . " with python files and RopeVim plugin?

I've installed the plugin RopeVim (using Pathogen) and it seems to be working.
Now when I call :RopeGoToDefinition with my vim cursor (in command mode) on a function I'd like to see the definition of...I get:
Rope project root folder: .
displayed in the status line of my vim (fwiw, I'm using MacVim).
What is the proper folder to specify here?
My project folder structure has a root folder, and various subdirs. I can't even tell if I should be specifying a system filepath or a python-style module.
See https://github.com/python-rope/rope/blob/master/docs/overview.rst#ropeproject-folder and the Getting Started section of https://github.com/python-rope/rope/blob/master/README.rst
You can stick it anywhere, but you probably want to use the root directory of your project. Note how the prompt defaults to ., the current directory. If your file is in the root directory, you can just hit Enter to accept that default. Otherwise, try something like ../.. or /path/to/root.

"ImportError: No module named" error doesn't seem to be related to my code

I am trying to complete Exercise 47 of Learn Python The Hard Way, 2nd Edition, but I am receiving this error when I run tests/ex47_tests.py:
File "tests/ex47_tests.py", line 3, in <module>
from ex47.game import Room
ImportError: No module named ex47.game
I thought it was something I was doing wrong within my code because I am very new at this, so I cloned this repo from a GitHub user who seems to have successfully completed the exercise.
Not only are the relevant parts of our code identical, but I receive the same error when I try to run the tests/ex47_tests.py that I cloned from him. So now I am lost and hoping that someone has a solution for me. Any ideas?
fabrizioM's answer should get it to work. Here is a little explanation.
When Python loads a file, it searches on the filesystem. So here, we have the import statement:
from ex47.game import Room
It looks for the file ex47.py on the modules search path (accessible as sys.path in Python code). The modules search path contains some directories based on the installation details of Python, the directories listed in the PYTHONPATH environment variable, and contains the parent directory of the script you’re executing. It doesn't find ex47.py on the path, but it sees there is a directory named ex47 with __init__.py inside of it. It then finds game.py in that folder.
The problem is that your current folder is not on the modules search path. Because ex47_tests.py was run, it has $cwd/tests on the path. You need $cwd on the path.
PYTHONPATH=. python tests/ex47_tests.py
does exactly that. It puts the $cwd on the modules search path so Python can find the source files.
You can also do:
python -m tests.ex47_tests
This will run it as a module instead of a file, while it will use the current directory as path it adds automatically to the the modules search path instead of the directory the file is located inside.
from the repository directory :
PYTHONPATH=. python tests/ex47_tests.py
Make sure there are no other ex47.py files/packages in your path.
The book asks you to copy the 'skeleton' directory and then use it for the game room exercise (#47). The skeleton directory has a "NAME" directory.
So if you copied the skeleton directory and named it ex47, you'll have another ex47 directory inside that.
lpthw > ex47 > ls
bin docs ex47 setup.py tests
So when the book says "Next, create a simple file ex47/game.py where you can put the code to test", you assume that it is the top level ex47. Not correct! That's why the import won't resolve.
I hit the same problem too until I realized that the book calls the outer directory (the one we got by copying 'skeleton') as 'simplegame' evident from this line in the book -
~/projects/simplegame $ nosetests
So all the answers about PYTHONPATH are valid, I just wanted to explain why the import wouldn't work for you!
There is something wrong with your directory structure. Here is my directory structure:
bin docs ex47 setup.py tests
./bin:
./docs:
./ex47:
game.py game.pyc __init__.py __init__.pyc
./tests:
ex47_tests.py ex47_tests.pyc __init__.py __init__.pyc
The project is named ex47 and there is a folder named ex47 within it.
(provided you have named your files according to his plan laid out before game.py file's code in this exercise)
When the author says create a simple file ex47/game.py he means the folder within is where the game.py file should be created and stored.
I too had the same error; being new to python, I followed the book example and after copying the skeleton directory over, renamed any files containing 'NAME' - one existed in the test/ directory and once removed the unable to load error disappeared - check the answer/contents in Microivan directory post above.
So within the project directory only two python files should exist, game.py and ex47_tests.py - hope you got there, on to the next exercise.!!

Categories