Running cpplint on all .cpp files - python

I want to run cpplint.py on all my .cpp files in a folder in Windows CMD.
py .\cpplint.py *.cpp
This somehow doesn't work. I get the Error:
Skipping input '*.cpp': Can't open for reading
Total errors found: 0
I thought * would be the operator for select all, am I wrong?
P.S.: There's a similar post, but it didn't really help.

Windows' CMD doesn't do wildcard expansion: https://superuser.com/questions/460598/is-there-any-way-to-get-the-windows-cmd-shell-to-expand-wildcard-paths
It's up to the application to handle the expansion of * into paths on Windows, which is in stark contrast to Linux, where the shell itself handles that. So you're passing the string-literal * to cpplint.py, which is not a file.

In a batch file or from the command line you can do:
for %%f in (*.cpp) do py .\cpplint.py %%f

Related

Python can't run absolute script?

I encountered such a weird situation:
naivechou#naivechou/~>python test.py
test
naivechou#naivechou/~>pwd
/home/naivechou
naivechou#naivechou/~>python /home/naivechou/test.py
C:\toolchain\python\python.exe: can't open file '/home/naivechou/test.py': [Errno 2] No such file or directory
My working directory is /home/naivechou/, test.py is in there.
If I run test.py with absolute path, I'll get an error message of No such file or directory.But everything will be fine if I enter that directory and then run it. What's wrong with python?
Try moving into the folder where the python script is located and do a "ls" command there in Linux. if windows then do 'dir'. if you see the required file there then execute the following command
C:\location_where_the_script_is> python yourfile.py
For commands entered on the command line, Windows doesn't recognize forward-slashes as directory separators.
Your second example is looking in the current directory for the literal filename /home/naivechou/test.py, and of course such a filename does not exist.
Use backslashes instead, as is the Windows way:
python \home\naivechou\test.py

Unpacking PyInstaller packed files

I currently have a PyInstaller packed Elf file and I'm looking to unpack it into the original .py file(s). I have been using PyInstaller Extractor but it appears to be telling the archive is not a PyInstaller archive.
Here is an example of what I've been doing:
$ cat main.py
#! /usr/bin/python3
print ("Hello %s" % ("World"))
I pack it in the file dist/main/main with the command:
pyinstaller main.py
Which outputs the file:
$ file dist/main/main
dist/main/main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=373ec5dee826653796e927ac3d65c9a8ec7db9da, stripped
Now, when I want to unpack it:
$ python pyinstxtractor.py dist/main/main
[*] Processing dist/main/main
[*] Error : Unsupported pyinstaller version or not a pyinstaller archive
I don't understand why the file cannot be unpacked while I've been looking through many posts telling that this should be possible and I'm beginning to doubt it.
Is the unpacking of the ELF file actually possible?
Am I doing it the right away?
According to the Github page, this script is applicable only for Windows binaries. There is an archive_viewer.py script distributed with pyinstaller itself that allows to view binary contents and extract it. If you get a .pyz file after extraction, use archive_viewer.py on it again. IIRC, after all you will get .pyc files, which have to be decompiled.
On my system (Manjaro Linux) I've found this script at /lib/python3.6/site-packages/PyInstaller/utils/cliutils
It is also available as pyi-archive_viewer (at /usr/bin/pyi-archive_viewer) after installing to global interpreter.
Using pyi-archive_viewer CLI seems to be the supported solution, i.e. to print only the module names, recursively, and quit instead of prompting:
$ pyi-archive_viewer --log --recursive --brief build/PYZ-00.pyz
['__future__',
'_aix_support',
---SNIP---
'zipfile',
'zipimport']
But if you don't want to parse or unsafely eval() the CLI output, it seems to work to use the library directly:
from PyInstaller.utils.cliutils import archive_viewer
archive = archive_viewer.get_archive('build/PYZ-00.pyz')
output = []
archive_viewer.get_content(archive, recursive=True, brief=True, output=output)
# Now, output is ['__future__', '_aix_support', ---SNIP--- 'zipfile', 'zipimport']
This use of the library is undocumented, but it's essentially the same to what the CLI does given those flags.

Calling 2to3 in Python using cmd

I have a folder of .py files written in Python 2.7 that I want to convert to Python 3 using the 2to3 tool. Using windows 10 in the cmd prompt i can convert a single file with the following command:
C:\Users\t\Desktop\search>python.exe 2to3.py -w graphicsDisplay.py
however this line is not syntactically correct when in python shell and ideally I'd like to be able to iterate through the whole folder and update all .py files using the by using the following python code in cmd:
C:\Users\t\Desktop\search>python
>>> import os
>>> for files in os.listdir('*filepath*'):
>>> if '.py' == str(files[-3:]):
>>> *...some line of code here to perform 2to3*
its the last line which I can't seem to get right so I guess my question is, how can I call the 2to3 function in python on each iteration of the files variable?
You can do it directly from command line
for %a in (*.py) do python.exe 2to3.py -w "%a"
For each file in the indicated set execute the conversion passing the for replaceable parameter (%a in this sample) that holds the reference to the file being iterated.
Looks like 2to3 support recursive folder checking if you leave out an explicit script to convert.
Would it be easier to have all your scripts in one folder and execute against that instead?
from: https://docs.python.org/2/library/2to3.html#
2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode

how do i get windows to execute .py with specific `python.exe`?

i tried in Control Panel\Programs\Default Programs\Set Associations, and also assoc/ftype, but windows keeps using the wrong python.exe (C:\Python27\) instead of the one i want (C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\)
>where python
C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\python.exe
C:\Python27\python.exe
C:\Program Files (x86)\LilyPond\usr\bin\python.exe
>assoc .py
.py=Python.File.WinPython
>ftype Python.File.WinPython
Python.File.WinPython=C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7
.9.amd64\python.exe "%1" %*
>set PATH
Path=C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64;...C:\Python27;C:\Python27\Scripts;...C:\Program Files (x86)\LilyPond\usr\bin;...
PATHEXT=.PY;...
i just made up Python.File.WinPython, is that allowed?
https://github.com/winpython/winpython/wiki/Installation#Registration
(but you will loose your python2.7 registration if you do that)
the other solution is :
call C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\scripts\env.bat
python my_progam_.py

python rename script not working when executed from finder (Mac)

Helly everyone,
I'm kind of new to python, but I have written a little script that helps me rename file extensions.
It's very short, so I'll post it here:
import os
for filename in os.listdir("."):
if filename.endswith("gd"):
os.rename(filename, filename + ".dat")
Now, as I have several directories I need to execute this in, it was getting kind of annoying to always navigate to the folder in terminal and execute it from there using the commmand
python renamescrip.py
Now I stumbled across some advice that would make a python script execute upon double clicking in the Finder, which would make life a lot easier for me, as I would just need to copy and paste the renamescript.py into the folder in which I want to rename the files and double click on it to execute it.
I followed these instructions: http://skillfulness.blogspot.de/2010/12/how-to-run-python-script-from-mac-os-x.html , which show how to create a .command file.
However, when I double clicked the renamescript.command file, it didn't change the file names in the directory.
I also tried opening the .py file with Python Launcher, but that also didn't change the filenames.
Both times a terminal window opened, though, with the message:
cd '/path to directory with files to rename/' && '/usr/local/bin/pythonw' '/path to directory with files to rename/renamescript.py' && echo Exit status: $? && exit 1
Exit status: 0
logout
or if I click on the .command file it's just:
/path to directory with files to rename/renamescript.command ; exit;
logout
What am I doing wrong?
When you double click, you are not running with the same directory form your current directory as when you run from the shell. This causes your os.listdir(".") to not find the files you think it should. You should change your directory at the beginning of your script with
os.chdir(os.path.dirname(os.path.abspath(__file__)))
before your os.listdir call

Categories

Resources