chiark / gitweb /
Fix the added to both but different conflict
[stgit] / stgit / utils.py
CommitLineData
41a6d859
CM
1"""Common utility functions
2"""
3
4__copyright__ = """
5Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License version 2 as
9published by the Free Software Foundation.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19"""
20
21def read_string(filename, multiline = False):
22 """Reads the first line from a file
23 """
24 f = file(filename, 'r')
25 if multiline:
7cc615f3 26 result = f.read()
41a6d859 27 else:
7cc615f3 28 result = f.readline().strip()
41a6d859 29 f.close()
7cc615f3 30 return result
41a6d859 31
7cc615f3
CL
32def write_string(filename, line, multiline = False):
33 """Writes 'line' to file and truncates it
41a6d859
CM
34 """
35 f = file(filename, 'w+')
36 if multiline:
7cc615f3 37 f.write(line)
41a6d859 38 else:
7cc615f3 39 print >> f, line
41a6d859
CM
40 f.close()
41
7cc615f3
CL
42def append_strings(filename, lines):
43 """Appends 'lines' sequence to file
680e3a32
PBG
44 """
45 f = file(filename, 'a+')
7cc615f3
CL
46 for line in lines:
47 print >> f, line
680e3a32
PBG
48 f.close()
49
7cc615f3
CL
50def append_string(filename, line):
51 """Appends 'line' to file
41a6d859
CM
52 """
53 f = file(filename, 'a+')
7cc615f3 54 print >> f, line
41a6d859
CM
55 f.close()
56
7cc615f3
CL
57def insert_string(filename, line):
58 """Inserts 'line' at the beginning of the file
41a6d859
CM
59 """
60 f = file(filename, 'r+')
61 lines = f.readlines()
62 f.seek(0); f.truncate()
7cc615f3 63 print >> f, line
41a6d859
CM
64 f.writelines(lines)
65 f.close()
66
67def create_empty_file(name):
68 """Creates an empty file
69 """
70 file(name, 'w+').close()