patternpythonMinor
Python program to lock and unlock files
Viewed 0 times
programfilespythonandunlocklock
Problem
The code takes
The function
Naturally, if the user wants to Lock the files, the file paths will be checked for unlockedness, and will proceed to lock those that are unlocked. If files are to be Unlocked, v
srcPaths and actionType as parameters. The former is a string with tab-delimited file paths, while the latter is a string that may have values of "Lock", "Unlock", and "Toggle", which is chosen by the user.#!/usr/bin/python
import os
import sys
import ntpath
import subprocess
srcPaths = sys.argv[1]
actionType = sys.argv[2]
srcPaths = srcPaths.split("\t")
srcPathsCount = len(srcPaths)
lockCount = unlockCount = 0
def fileIsLocked(srcPath):
cmd1 = subprocess.Popen(["ls", "-ldO", srcPath], stdout=subprocess.PIPE)
cmd2 = subprocess.Popen(["awk", "{ print $5 }"], stdin=cmd1.stdout, stdout=subprocess.PIPE)
cmd1.stdout.close()
if "uchg" in cmd2.communicate()[0]:
return True
else:
return False
def setLock(flagStr, srcPath):
global lockCount, unlockCount
subprocess.call(["chflags", flagStr, srcPath])
if flagStr == "uchg":
lockCount += 1
else:
unlockCount += 1
for srcPath in srcPaths:
if actionType == "Lock":
if not fileIsLocked(srcPath):
setLock("uchg", srcPath)
elif actionType == "Unlock":
if fileIsLocked(srcPath):
setLock("nouchg", srcPath)
else:
if fileIsLocked(srcPath):
setLock("nouchg", srcPath)
else:
setLock("uchg", srcPath)
sys.stdout.write("{}/{}/{}".format(lockCount, unlockCount, srcPathsCount))The function
fileIsLocked(srcPath) checks if the file is locked. It does this by using ls -ldO and awk to get the file flags. If it finds "uchg" there, then the function returns True, and vice versa. The function setLock(flagStr, srcPath) executes the shell script command to lock/unlock the file depending on the conditions, and keeps count of files that have been locked and unlocked.Naturally, if the user wants to Lock the files, the file paths will be checked for unlockedness, and will proceed to lock those that are unlocked. If files are to be Unlocked, v
Solution
-
I have absolutely no idea what the purpose of this code is. Something to do with locking and unlocking files, but why? Your code doesn't say, and neither does your post. It's hard to review code when I have no idea what it is supposed to do.
-
There are no docstrings. What do these functions do? What arguments should I pass?
-
Running shell commands and parsing their output is complex and slow. Instead, get the flags for a file by calling
To read the flags for a file:
To determine if a file is immutable, evaluate:
To set the immutable flag, call:
To clear the immutable flag, call:
-
Since you don't say what the purpose of this code is, I can't tell if it is a good implementation or not. If you are planning to use this mechanism for mutual exclusion (e.g. to prevent two instances of a program running at the same time) then it won't work, because it has race conditions (the flags might change between getting them and setting them).
I have absolutely no idea what the purpose of this code is. Something to do with locking and unlocking files, but why? Your code doesn't say, and neither does your post. It's hard to review code when I have no idea what it is supposed to do.
-
There are no docstrings. What do these functions do? What arguments should I pass?
-
Running shell commands and parsing their output is complex and slow. Instead, get the flags for a file by calling
os.stat and set the flags for a file by calling os.chflags. Use the constants in the stat module, for example stat.UF_IMMUTABLE for the uchg flag.To read the flags for a file:
import os, stat
flags = os.stat(filename).st_flagsTo determine if a file is immutable, evaluate:
bool(flags & stat.UF_IMMUTABLE)To set the immutable flag, call:
os.chflags(filename, flags | stat.UF_IMMUTABLE)To clear the immutable flag, call:
os.chflags(filename, flags & ~stat.UF_IMMUTABLE)-
Since you don't say what the purpose of this code is, I can't tell if it is a good implementation or not. If you are planning to use this mechanism for mutual exclusion (e.g. to prevent two instances of a program running at the same time) then it won't work, because it has race conditions (the flags might change between getting them and setting them).
Code Snippets
import os, stat
flags = os.stat(filename).st_flagsbool(flags & stat.UF_IMMUTABLE)os.chflags(filename, flags | stat.UF_IMMUTABLE)os.chflags(filename, flags & ~stat.UF_IMMUTABLE)Context
StackExchange Code Review Q#129455, answer score: 5
Revisions (0)
No revisions yet.