Initializing Repo Object With Git Package in Python - python

I have been working for days on how to check if a file is in a specific directory, and I narrowed the question down the following: initialize a repo object with the git package in python?
I have looked elsewhere and have found the following code: git.Repo.init(self.repo_path). But what is repo_path? How do you grab it? Alternatively, some people use os.path.exists(filePathAndName), but this still does not help. How do you get the filePathAndName? And does this solution assume that your github is downloaded to your desktop?
In essence, the code on the internet seems to lack context, but I am sure I am missing something. Any help would be very much appreciated.

repo_path is the name of the variable, whose value is the path to the repo/directory. You took this part out of its context, where self.repo_path is surely defined. The same goes for filePathAndName.
Using Git, based on the description of your problem, seems irrelevant. And it lead you to an XY problem : "how to initialize a git repo" instead of your actual problem "how to check if file exists".
You seem to lack programming knowledge, notably how variables work. Here is a tutorial about them.
In short, you can associate values to certain names. For example :
filePathAndName = 'path/to/file.txt' # storing the value inside the variable
os.path.exists(filePathAndName) # using the variable as parameter to a function call
which is basically the same as :
os.path.exists('path/to/file.txt') # using the value directly for the function
and is a correct way to get your answer.
If you meant to check if a certain file exists in a Git repository, it is a bit more difficult because the file could exist in a different branch, currently not existing on your filesystem. So a Git library may be of use there to get this info.
Here is an example based on the GitPython's tutorial :
# having installed the library "GitPython" beforehand
from git import Repo
path_to_my_file = 'path/to/repo/somewhere/inside/there/is/file.txt'
path_to_my_repo = 'path/to/repo'
my_repo = Repo(path_to_my_repo)
# iterate over the repo's branches to check if they contain your file
I hope this clarifies the subject we are talking about.

Related

Download latest version from Github, unzip to folder and overwrite contents?

I have a Node project that's bundled and added to Github as releases. At the moment, it checks my Github for a new release via the API and lets the user download it. The user must then stop the Node server, unzip the release.zip to the folder and overwrite everything to update the project.
What I'm trying to do is write a Python script that I can execute in Node by spawning a new process. This will then kill the Node server using PM2, and then Python script will then check the Github API, grab the download url, downloads it, unzips the contents to the current folder, deletes the zip and then starts up the Node server again.
What I'm struggling with though is checking the Github API and downloading the latest release file. Can anyone point me in the right direction? I've read that wget shouldn't be used in Python, and instead use urlopen
If you are asking for ways to get data from a web server, the two main libraries are:
Requests
Urllib
Personally, I prefer requests. They both have good documentation.
With requests, getting JSON data is as simple as:
r = requests.get("example.com")
r = r.json()
You can add headers and other information easily, though keep in mind that while it supports HTTP, it doesn't support HTTPS.
You need to map out your workflow and dataflow better. You can do it in words or pictures. If you can express your problem clearly and completely in words step by step in list format in words, then translate it to pseudocode. Python is great because you can go almost immediately from a good written description, to pseudocode, to a working implementation. Then at least you have something that works, and you can optimize performance, simplify functionality or usability from there. This is the process of translating a problem into a solution.
When asking questions on SO, you need to show your current thinking, what you've already tried, preferably with your code that doesn't yet work, or work the way you need it to work. People can vote you down and give you negative reputation points if you ask a question with just a vague description, a question that is an obvious cry for help with homework (yours is not that), or a muse or a vague question with not even an attempt at a solution, because it does not contribute back to the community in any way.
Do you have any code or detailed pseudocode steps for checking the GitHub API and checking for the "latest release" of file(s) you are trying to update?

How do I find out what I can do with the Pelican signals? I'm trying to write a plugin

The signals are sort-of described here but, then what?
For example, one of the signals is desribed:
invoked before writing each article, the article is passed as content
How do I change that content? How do I access it? What functions are available?
I've been looking at examples in the pelican plugins repo on github, but I'm still confused. (How did those people even learn how to write those plugins?)
I hardly know where to start.
You have to look at the source code of pelican. I think there is no better way.
For example, search for the signal you are interested in, e.g. article_generator_write_article: https://github.com/getpelican/pelican/search?utf8=%E2%9C%93&q=article_generator_write_article
Then, look into the search result, e.g. generators.py and click on the line number containing your signal. Of course, you could also create a clone and do all this locally. This depends on your way of working.
Surrounding code:
def generate_articles(self, write):
"""Generate the articles."""
for article in chain(self.translations, self.articles):
signals.article_generator_write_article.send(self, content=article)
write(article.save_as, self.get_template(article.template),
self.context, article=article, category=article.category,
override_output=hasattr(article, 'override_save_as'), blog=True)
As you can see, the signal call provides you with an article object. You can now 1) look in the source code to find the respective python class of this object to find out about its inner workings, methods and attributes or 2) go the hacky path and simply print the object's members print(article.__dict__).
I suppose, without having looked in the code, that article has an attribute content which contains the HTML code generated from your source file. This is where your desired change comes in.
Note that if you want to change the source code before processing this is not that easy. I just wrote a little plugin which is capable of doing this.
There you can also see the signal API in action. You simply have to connect a handler function to the desired signal.
I hope this helps :)

Plugin not working on MusicBrainz v1.2

As an exercise for learning, I am trying to write a plugin for MusicBrainz that matches the albumartistsort to albumartist and artistsort to artist, as opposed to the (apparently) default of Last Name, First Name format it is currently using.
I am just learning about Python and therefore I am trying to use another plugin as a guide, but some important changes need to be done and that's where I probably screwed up.
When I try installing the plug in, it doesn't appear in the plugin list although it is copied to the plugin folder; and the .pyo file is not generated. I am guessing this is due to a compilation error, but I haven't been able to include whatever I need so I can use the picard module (don't know where to find it nor import it) so I can test in my python interpreter.
This is the code I have:
PLUGIN_NAME = "Sort Artist and Album Artist"
PLUGIN_AUTHOR = "Kevin Hernandez"
PLUGIN_DESCRIPTION = "Sorts artist/album artist by name as in Artist/Album Artist field instead of Last, First"
PLUGIN_VERSION = "0.1"
PLUGIN_API_VERSIONS = ["0.9.0", "0.10", "0.15", "0.16"]
from picard.metadata import register_album_metadata_processor
import re
def copy_albumartist_to_albumartistsort(tagger, metadata, release):
match = re.search($not($eq(metadata["albumartistsort"],metadata["albumartist"])))
if match:
metadata["albumartistsort"] = metadata["albumartist"]
def copy_artist_to_artistsort(tagger, metadata, release):
match = re.search($not($eq(metadata["artistsort"],metadata["artist"])))
if match:
metadata["artistsort"] = metadata["artist"]
register_album_metadata_processor(copy_albumartist_to_albumartistsort)
register_album_metadata_processor(copy_artist_to_artistsort)
and I also tried defining the functions as:
def copy_albumartist_to_albumartistsort(tagger, metadata, release):
metadata["albumartistsort"] = metadata["albumartist"]
def copy_artist_to_artistsort(tagger, metadata, release):
metadata["artistsort"] = metadata["artist"]
I must point out that I don't fully understand when these are called. I believe the plugin documentation here, here and here is not enough to follow the plugins they have there (e.g. the search and match methods they use in different plugins with re are not explained in the documentation links I am referring to.
If there's a more thorough documentation for it, you can pinpoint what I am doing wrong in my code, or know how to include the picard module in an interpreter (where to find it AND how to include it), then your comments are very much appreciated and valid answers to this question.
I think your biggest problem is that you're mixing up the plugin API with the tagger scripting language.
Tagger scripts are written in a simple custom language; plugins are written in Python. You can't mix and match syntax between the two languages. In particular:
match = re.search($not($eq(metadata["albumartistsort"],metadata["albumartist"])))
That $not, $eq, etc. doesn't mean anything in Python. If you want to check whether things are equal, you use the == operator. If you want to use re.search, you use regular expression syntax. And so on.
Also, your code has to be valid Python, with valid indentation. Your code, at least as posted here.
But let's go through your questions one by one:
I am trying to write a plugin for MusicBrainz that matches the albumartistsort to albumartist and artistsort to artist, as opposed to the (apparently) default of Last Name, First Name format it is currently using.
There are very few automatic defaults in MusicBrainz. Each artist has a name and a sort name, in the database, entered by a human user and verified by other users. You can see this from the web interface. For example, go to David Bowie, and in the "Artist Information" panel on the right side, you'll see "Sort name: Bowie, David". If you're not used to using MusicBrainz's web interface, you should explore it before trying to expand Picard.
When I try installing the plug in, it doesn't appear in the plugin list although it is copied to the plugin folder; and the .pyo file is not generated. I am guessing this is due to a compilation error
Yep. If you run Picard from the command-line with the -d flag, it will show you the errors instead of just silently disabling your plugin, so you don't have to guess. This is documented under Troubleshooting. (If you're on a Mac, the path will be something like /Applications/MusicBrainz Picard.app/Contents/MacOS/MusicBrainz Picard; I think the docs don't explain that because it's standard OS X app bundle stuff.)
but I haven't been able to include whatever I need so I can use the picard module (don't know where to find it nor import it) so I can test in my python interpreter.
You really can't test it in your interpreter. Picard bundles its own custom-built Python interpreter, rather than using your system Python. In that custom interpreter, the picard package is on the sys.path, but in your system Python interpreter, it's not. And trying to import that package and use things out of it while not actually running the Picard GUI wouldn't be a good idea anyway.
If you really want to explore what's in the picard package, download the source and run a local build of the code. But you really shouldn't need to do that. You shouldn't need any functions beyond those documented in the API, and if you want to debug things, you want to debug them in the right context, which generally means adding print functions and/or using the logging module in your code.
I must point out that I don't fully understand when these are called.
At some point after each album is downloaded from the MusicBrainz server, all registered album processor functions get called with the album, and all registered track processor function get called with each track on the album.
Notice that an album processor isn't going to be able to change track-level fields like the track artist sort; you will need a track processor for that.
e.g. the search and match methods they use in different plugins with re are not explained in the documentation links I am referring to.
That's because they're part of the Python standard library, which is documented as part of the standard Python docs—in this case, see re.
You're expected to know the basics of Python before being able to write a Picard plugin.
Meanwhile, I'm not sure what you were trying to write here, but it looks like a really convoluted attempt to say "if these two fields aren't equal, make them equal". Which does the same thing as "unconditionally make them equal". So, why even bother with the regexp and the if condition?
So, your functions could be simplified to:
def copy_albumartist_to_albumartistsort(tagger, metadata, release):
metadata["albumartistsort"] = metadata["albumartist"]
def copy_artist_to_artistsort(tagger, metadata, release, track):
metadata["artistsort"] = metadata["artist"]
register_album_metadata_processor(copy_albumartist_to_albumartistsort)
register_track_metadata_processor(copy_artist_to_artistsort)
However, you really don't need a plugin here at all. You should be able to write this whole thing as a trivial tagger script:
$set(artistsort,%artist%)
$set(albumartistsort,%albumartist%)

Python pefile member question

Gang,
I apologize if this is a really dumb question... I am wanting to use the super convenient python script pefile (http://code.google.com/p/pefile/) that parses an executable and lists particular information about the PE structure. My question is where can I find information about how to access particular members of the executable? I've scoured the wiki and read the usage examples but that documentation only covered 4-5 members. What I am wondering is if you guys have a list of members I can access to display the information I care about. So specifically, if I wanted to list the Stack Commit Size of an executable, does it look like this: pe.FILE_HEADER.StackCommitSize, obviously I can run this code and figure it out but have you guys seen API DOC floating around that I find the members i need?
THANKS!
From the PE docstring:
Basic headers information will be available in the attributes:
DOS_HEADER
NT_HEADERS
FILE_HEADER
OPTIONAL_HEADER
All of them will contain among their attributes the members of the
corresponding structures as defined in WINNT.H
So, look at winnt.h and you'll see which attributes are available.
Or just read the source code for the module. It's big, but everything you need to know is in there.
You can generally find everything that is in a PE file in the Microsoft PE/COFF specification.
Once you've looked there, you know that the StackCommitSize is in the optional image header. Then all you have to do is to look for the corresponding structure in pefile, which usually bears a similar name, if not indeed the very same name. In this case:
pe = pefile.PE("C:\\Windows\\Notepad.exe")
print pe.OPTIONAL_HEADER.SizeOfStackCommit
Will give you what you want.
If you have trouble finding SizeOfStackCommit (after you've found it in the specification), just use your quick find on the source code. It's as easy to read as you can get, and I don't think you'll have any trouble finding the required structure.
Now, there probably aren't any API docs for pefile itself, but as you can see there's really no need for it, since it's just a nice Pythonic wrapper around the PE specification itself.

How can I have my python file show its mercurial tag or revision as the module version?

I'd like to add a --version command line option to my python application that will show the right version depending on the tagged status of the command:
If the file comes from a version whose short hex ID was abcdef01 that was tagged TAG, --version should show this:
MyApp Version TAG (abcdef01)
If the file comes from the tip, --version should show this:
MyApp (tip)
If the file comes from an arbitrary, untagged revision abcdef02, --version should show this:
MyApp (development, abcdef02)
Is this possible? If so, how?
Once you activate the keyword extension, you can have it in a variable which can be carved up for the hash.
Someone pointed out the KeywordExtension, and that's definitely one route to go.
For a little more control you can create an 'update' writes what you want into a version file which you don't add to the repository itself. Something like this in your repo's hgrc:
[hooks]
update = hg id > version.txt
Where version.txt exists in your .hgignore because you don't want to track changes to it. Then you have your version code read that file.
The advantage to using a hook vs. the KeywordExtension is the ability to use more complex tag trolling logic.
In fact the nearest extension might get you pretty much exactly what you want:
The goal of the nearest mercurial
extension is to find the nearest
tag(s) from a given changeset, either
backward or forward in the changesets
history tree.
By default, tags are searched backward
in history, but using the --contains
option will make it search forward. It
answers the following questions:
On which tag this changeset is based
on ? (without --contains) Which tag
will include / contain this changeset
? (with --contains) The tags are
searched by date, so that the nearest
tag in time will be reported. However,
the --all option will make the
extension search the first tag on all
possible branches.

Categories

Resources