Back to index

python3.2  3.2.2
Functions | Variables
treesync Namespace Reference

Functions

def main
def process
def compare
def identical
def mtime
def funnychars
def copy
def raw_input
def okay

Variables

string default_answer = "ask"
string create_files = "yes"
string create_directories = "no"
string write_slave = "ask"
string write_master = "ask"
int BUFSIZE = 16

Function Documentation

def treesync.compare (   slave,
  master 
)

Definition at line 109 of file treesync.py.

00109 
00110 def compare(slave, master):
00111     try:
00112         sf = open(slave, 'r')
00113     except IOError:
00114         sf = None
00115     try:
00116         mf = open(master, 'rb')
00117     except IOError:
00118         mf = None
00119     if not sf:
00120         if not mf:
00121             print("Neither master nor slave exists", master)
00122             return
00123         print("Creating missing slave", slave)
00124         copy(master, slave, answer=create_files)
00125         return
00126     if not mf:
00127         print("Not updating missing master", master)
00128         return
00129     if sf and mf:
00130         if identical(sf, mf):
00131             return
00132     sft = mtime(sf)
00133     mft = mtime(mf)
00134     if mft > sft:
00135         # Master is newer -- copy master to slave
00136         sf.close()
00137         mf.close()
00138         print("Master             ", master)
00139         print("is newer than slave", slave)
00140         copy(master, slave, answer=write_slave)
00141         return
00142     # Slave is newer -- copy slave to master
00143     print("Slave is", sft-mft, "seconds newer than master")
00144     # But first check what to do about CRLF
00145     mf.seek(0)
00146     fun = funnychars(mf)
00147     mf.close()
00148     sf.close()
00149     if fun:
00150         print("***UPDATING MASTER (BINARY COPY)***")
00151         copy(slave, master, "rb", answer=write_master)
00152     else:
00153         print("***UPDATING MASTER***")
00154         copy(slave, master, "r", answer=write_master)

Here is the call graph for this function:

Here is the caller graph for this function:

def treesync.copy (   src,
  dst,
  rmode = "rb",
  wmode = "wb",
  answer = 'ask' 
)

Definition at line 176 of file treesync.py.

00176 
00177 def copy(src, dst, rmode="rb", wmode="wb", answer='ask'):
00178     print("copying", src)
00179     print("     to", dst)
00180     if not okay("okay to copy? ", answer):
00181         return
00182     f = open(src, rmode)
00183     g = open(dst, wmode)
00184     while 1:
00185         buf = f.read(BUFSIZE)
00186         if not buf: break
00187         g.write(buf)
00188     f.close()
00189     g.close()

Here is the call graph for this function:

def treesync.funnychars (   f)

Definition at line 169 of file treesync.py.

00169 
00170 def funnychars(f):
00171     while 1:
00172         buf = f.read(BUFSIZE)
00173         if not buf: break
00174         if '\r' in buf or '\0' in buf: return 1
00175     return 0

Here is the caller graph for this function:

def treesync.identical (   sf,
  mf 
)

Definition at line 157 of file treesync.py.

00157 
00158 def identical(sf, mf):
00159     while 1:
00160         sd = sf.read(BUFSIZE)
00161         md = mf.read(BUFSIZE)
00162         if sd != md: return 0
00163         if not sd: break
00164     return 1

Here is the caller graph for this function:

def treesync.main ( void  )

Definition at line 35 of file treesync.py.

00035 
00036 def main():
00037     global always_no, always_yes
00038     global create_directories, write_master, write_slave
00039     opts, args = getopt.getopt(sys.argv[1:], "nym:s:d:f:a:")
00040     for o, a in opts:
00041         if o == '-y':
00042             default_answer = "yes"
00043         if o == '-n':
00044             default_answer = "no"
00045         if o == '-s':
00046             write_slave = a
00047         if o == '-m':
00048             write_master = a
00049         if o == '-d':
00050             create_directories = a
00051         if o == '-f':
00052             create_files = a
00053         if o == '-a':
00054             create_files = create_directories = write_slave = write_master = a
00055     try:
00056         [slave, master] = args
00057     except ValueError:
00058         print("usage: python", sys.argv[0] or "treesync.py", end=' ')
00059         print("[-n] [-y] [-m y|n|a] [-s y|n|a] [-d y|n|a] [-f n|y|a]", end=' ')
00060         print("slavedir masterdir")
00061         return
00062     process(slave, master)

Here is the call graph for this function:

Here is the caller graph for this function:

def treesync.mtime (   f)

Definition at line 165 of file treesync.py.

00165 
00166 def mtime(f):
00167     st = os.fstat(f.fileno())
00168     return st[stat.ST_MTIME]

Here is the caller graph for this function:

def treesync.okay (   prompt,
  answer = 'ask' 
)

Definition at line 195 of file treesync.py.

00195 
00196 def okay(prompt, answer='ask'):
00197     answer = answer.strip().lower()
00198     if not answer or answer[0] not in 'ny':
00199         answer = input(prompt)
00200         answer = answer.strip().lower()
00201         if not answer:
00202             answer = default_answer
00203     if answer[:1] == 'y':
00204         return 1
00205     if answer[:1] == 'n':
00206         return 0
00207     print("Yes or No please -- try again:")
00208     return okay(prompt)

Here is the call graph for this function:

Here is the caller graph for this function:

def treesync.process (   slave,
  master 
)

Definition at line 63 of file treesync.py.

00063 
00064 def process(slave, master):
00065     cvsdir = os.path.join(master, "CVS")
00066     if not os.path.isdir(cvsdir):
00067         print("skipping master subdirectory", master)
00068         print("-- not under CVS")
00069         return
00070     print("-"*40)
00071     print("slave ", slave)
00072     print("master", master)
00073     if not os.path.isdir(slave):
00074         if not okay("create slave directory %s?" % slave,
00075                     answer=create_directories):
00076             print("skipping master subdirectory", master)
00077             print("-- no corresponding slave", slave)
00078             return
00079         print("creating slave directory", slave)
00080         try:
00081             os.mkdir(slave)
00082         except os.error as msg:
00083             print("can't make slave directory", slave, ":", msg)
00084             return
00085         else:
00086             print("made slave directory", slave)
00087     cvsdir = None
00088     subdirs = []
00089     names = os.listdir(master)
00090     for name in names:
00091         mastername = os.path.join(master, name)
00092         slavename = os.path.join(slave, name)
00093         if name == "CVS":
00094             cvsdir = mastername
00095         else:
00096             if os.path.isdir(mastername) and not os.path.islink(mastername):
00097                 subdirs.append((slavename, mastername))
00098     if cvsdir:
00099         entries = os.path.join(cvsdir, "Entries")
00100         for e in open(entries).readlines():
00101             words = e.split('/')
00102             if words[0] == '' and words[1:]:
00103                 name = words[1]
00104                 s = os.path.join(slave, name)
00105                 m = os.path.join(master, name)
00106                 compare(s, m)
00107     for (s, m) in subdirs:
00108         process(s, m)

Here is the call graph for this function:

Here is the caller graph for this function:

def treesync.raw_input (   prompt)

Definition at line 190 of file treesync.py.

00190 
00191 def raw_input(prompt):
00192     sys.stdout.write(prompt)
00193     sys.stdout.flush()
00194     return sys.stdin.readline()


Variable Documentation

Definition at line 155 of file treesync.py.

Definition at line 31 of file treesync.py.

Definition at line 30 of file treesync.py.

Definition at line 29 of file treesync.py.

Definition at line 33 of file treesync.py.

Definition at line 32 of file treesync.py.