Archive
Handle extended file attributes
Problem
You want to handle extended file attributes from Python.
Solution
If you are not familiar with extended file attributes, you can read more about it here. I also have a blog post (see here), in which I show how to set/remove extended file attributes using the attr Linux command.
To handle the extended file attributes from Python, you can use the pyxattr package: https://github.com/iustin/pyxattr
Its usage is very simple:
>>> import xattr
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs']
>>> xattr.getxattr("main.py", "user.com.dropbox.attrs")
b'\n\x12...'
>>> xattr.setxattr("main.py", "user.com.dropbox.ignored", "1")
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs', b'user.com.dropbox.ignored']
>>> xattr.getxattr("main.py", "user.com.dropbox.ignored")
b'1'
>>> xattr.removexattr("main.py", "user.com.dropbox.ignored")
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs']
A typical use case: you want Dropbox to ignore your venv/ folder inside your project.
Links
- https://en.wikipedia.org/wiki/Extended_file_attributes
- https://help.dropbox.com/sync/ignored-files
- https://github.com/iustin/pyxattr (I used this in this post)
- https://github.com/xattr/xattr (an alternative)
Static HTML file browser for Dropbox
Two of my students worked on a project that creates static HTML files for a public Dropbox folder (find it at github). I use it in production, check it out here.
If you created your Dropbox account before October 4, 2012, then you are lucky and you have a Public folder. Accounts opened after this date have no Public folder :(
So, if you have a Public folder and you want to share the content of a folder recursively, then you can try this script. It produces a file and directory list that is similar to an Apache output.
Screenshot
Authors and Contributors
- Kiss Sándor Ádám (main developer)
- Iváncza Csaba (junior developer)
- Jabba Laci (project idea)
virtualenvwrapper (Part 2)
Here I describe how I use virtualenvwrapper.
I keep my virtual environments in the default location (~/.virtualenvs folder). My Python projects are kept in Dropbox. If a project (call it “stuff” for instance) has a virtual env., then I add the suffix “_project” to the projects’s folder name (resulting “stuff_project” in this example). This way I can see it immediately (without entering the folder) that it has an attached venv.
In the stuff_project folder create a requirements.txt file that contains all the modules that you installed in your venv. This way you can re-create your venv on another machine and continue to work on yout project.
Let’s see a concrete example. Say I want to create a new Django project:
$ cd $HOME/Dropbox/python/webapps $ mkdir stuff_project # ^ every project file will be in this folder $ cd stuff_project $ django-admin.py startproject stuff # ^ create the Django project $ mkvirtualenv stuff_project # ^ venv is created AND activated $ pip install django $ pip install django-extensions # ^ useful extensions $ pip freeze --local >requirements.txt # ^ packages installed in the venv are listed in this file
Since I put the project folder in Dropbox, it will be synced on all my machines. What to do if I want to work on this project on another machine of mine (let’s call it “machine #2”)? The virtual environment will be missing. But there is no need to worry. We have a requirements.txt file and from this we can populate a newly created venv. Install virtualenvwrapper on machine #2 and create an empty venv for the project:
# actions to perform on machine #2: $ cd $HOME/Dropbox/python/webapps/stuff_project $ mkvirtualenv stuff_project # ^ the venv is created and activated $ pip install -r requirements.txt # ^ packages are installed in the venv
When you are done with the project, deactivate the venv with the “deactivate” command. Since it’s too long, I put an alias on it:
alias off='deactivate'
If you install new packages in the venv, don’t forget to update the requirements.txt file ( pip freeze >requirements.txt )!


You must be logged in to post a comment.