Kiln » Kiln Storage Service Read More
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

tip update to Kiln Storage Service 2.5.139

Changeset c5ec70d1ed42

Parent fca1d86acdc9

by Profile picture of User 12Benjamin Pollack <benjamin@fogcreek.com>

Changes to 42 files · Browse files at c5ec70d1ed42 Showing diff from parent fca1d86acdc9 Diff from another changeset...

Change 1 of 1 Show Entire File .hgignore Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@@ -0,0 +1,18 @@
+syntax: glob +*.pyc +*.pyo +*.swp +*.db +*.sqlite3 +*.orig +kiln/dist/* +TAGS +\#*\# +local_settings.py +kiln/build/* +installer/Output +out.txt +*~ +_ReSharper.* +obj +bin
Change 1 of 1 Show Entire File README Stacked
 
1
2
3
 
4
5
6
 
1
2
 
3
4
5
6
@@ -1,6 +1,6 @@
 OVERVIEW   -This is a stand-alone server for Mercurial repositories, that provides +This is a stand-alone server for Mercurial repositories that provides  Mercurial data in the form of JSON requests. This allows for much  more efficient polling of repository data from long-running  applications, such as websites, IDEs, and so on.
Change 1 of 2 Show Entire File build.ps1 Stacked
 
1
2
 
 
 
 
 
 
 
 
3
4
5
 
10
11
12
 
 
 
 
13
 
 
 
 
14
15
 
16
17
18
 
1
2
3
4
5
6
7
8
9
10
11
12
13
 
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@@ -1,5 +1,13 @@
 param([string] $repopath = "..")   +function Get-Batchfile ($file) { + $cmd = "`"$file`" & set" + cmd /c $cmd | Foreach-Object { + $p, $v = $_.split('=') + Set-Item -path env:$p -value $v + } +} +  function Get-ScriptDirectory  {   $Invocation = (Get-Variable MyInvocation -Scope 1).Value @@ -10,9 +18,18 @@
   pushd $path  pushd kiln +if (test-path 'c:\pythonve\kiln25') +{ + Get-Batchfile('c:\pythonve\kiln25\scripts\activate.bat') +}  python setup.py py2exe +if (test-path 'c:\pythonve\kiln25') +{ + Get-Batchfile('c:\pythonve\kiln25\scripts\deactivate.bat') +}  hg -R $repopath archive -t zip dist\source.zip  popd +c:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe /p:Configuration=Release installer\RepoDirectoryMigrator\RepoDirectoryMigrator.sln  $iscc = "C:\Program Files (x86)\Inno Setup 5\ISCC.exe"  if (-not (Test-Path $iscc))  {
 
1
2
 
3
4
5
 
33
34
35
 
 
 
36
37
38
 
39
 
 
 
 
40
41
42
43
44
45
46
47
48
 
56
57
58
 
 
 
 
59
60
 
 
 
61
62
63
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
66
67
 
72
73
74
 
75
76
77
 
133
134
135
 
 
136
137
 
 
138
139
140
 
149
150
151
 
 
152
 
 
 
 
 
 
153
154
155
156
157
 
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
160
161
162
163
 
 
 
 
 
 
 
 
 
 
 
164
165
166
167
168
169
170
 
 
 
 
 
171
172
173
 
183
184
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
3
4
5
 
33
34
35
36
37
38
39
 
40
41
42
43
44
45
46
47
48
49
50
 
 
51
52
53
 
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
 
105
106
107
108
109
110
111
 
167
168
169
170
171
172
173
174
175
176
177
178
 
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
 
249
250
251
252
253
254
255
256
 
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
@@ -1,5 +1,5 @@
 #define MyAppName "Kiln Storage Service" -#define MyAppVerName "Kiln Storage Service 1.0" +#define MyAppVerName "Kiln Storage Service 2.5"  #define MyAppPublisher "Fog Creek Software"  #define MyAppURL "http://www.fogcreek.com/kiln/"   @@ -33,16 +33,21 @@
 WelcomeLabel2=This will install [name/ver] on your computer.    [Files] + +Source: RepoDirectoryMigrator\RepoDirectoryMigrator\bin\x86\Release\RepoDirectoryMigrator.exe; DestDir: {tmp}; Flags: ignoreversion +Source: ctags.exe; DestDir: {app}; Flags: ignoreversion  Source: ..\kiln\dist\library.zip; DestDir: {app}; Flags: ignoreversion -Source: ..\kiln\dist\w9xpopen.exe; DestDir: {app}; Flags: ignoreversion  Source: ..\kiln\dist\backend.exe; DestDir: {app}; Flags: ignoreversion +Source: ..\kiln\redis-server.exe; DestDir: {app}; Flags: ignoreversion  Source: ..\kiln\dist\source.zip; DestDir: {app}; Flags: ignoreversion +Source: ..\kiln\dist\opengrok.jar; DestDir: {app}\opengrok; Flags: ignoreversion +Source: ..\kiln\dist\lib\*; DestDir: {app}\opengrok\lib; Flags: recursesubdirs replacesameversion; Excludes: .hg*,*~ +Source: ..\kiln\client.crt; DestDir: {app}; Flags: ignoreversion +Source: ..\kiln\client.key; DestDir: {app}; Flags: ignoreversion    [Icons]  Name: {group}\{cm:UninstallProgram,{#MyAppName}}; Filename: {uninstallexe}   -[Run] -Filename: {app}\backend.exe; Parameters: --startup auto install; StatusMsg: Registering Kiln Storage Service; Flags: runhidden  [UninstallRun]  Filename: {app}\backend.exe; Parameters: stop; StatusMsg: Stopping Kiln Storage Service; Flags: runhidden  Filename: {app}\backend.exe; Parameters: remove; StatusMsg: Removing Kiln Storage Service; Flags: runhidden @@ -56,12 +61,40 @@
  StorageLocation: String;   Port: Cardinal;   + JavaVersion: String; + + StoppedOldService: Boolean; +  const   REG_KEY = 'Software\Fog Creek Software\Kiln'; + OG_KEY = 'Software\Fog Creek Software\Kiln\OpenGrok'; + DAEMON_KEY = 'Software\Fog Creek Software\Kiln\Daemon'; + JAR = 'Jar';   BACKEND_IP = 'KilnBackendIP';   BACKEND_PORT = 'KilnBackendPort';   REPOSITORY_ROOT = 'KilnRepositoryRoot';   DELIBERATELY_PUBLIC = 'KilnDeliberatelyPublic'; + MINIREDIS_DB = 'MiniredisDB'; + DATA_DIR = 'DataDir'; + + INDEX_THREADS = 'IndexThreads'; + QUEUE_THREADS = 'QueueThreads'; + NINDEX_THREADS = 1; + NQUEUE_THREADS = 1; + + DAEMON_HOST = 'host'; + DAEMON_PORT = 'port'; + DAEMON_DB = 'db'; + DAEMON_SSL_KEY = 'ssl_key'; + DAEMON_SSL_CERT = 'ssl_cert'; + + JAVA_KEY = 'Software\JavaSoft\Java Runtime Environment'; + JAVA_VERSION = 'CurrentVersion'; + + JAVA = 'Java'; + CONFIG_UPDATE = 'ConfigUpdate'; + JAVA_HOME = 'JavaHome'; + CTAGS = 'CTags';    procedure InitializeWizard;  var @@ -72,6 +105,7 @@
  nextPageParent: Integer;   param: String;  begin + StoppedOldService := False;   LocalOnly := False;   for idx := 0 to ParamCount do   begin @@ -133,8 +167,12 @@
 procedure FinishInstall;  var   ip: String; + ogStorageLocation: String; + MiniredisDBLocation: String;   ResultCode: Integer;   deliberatelyPublic: Cardinal; + JavaLoc: String; + ret: Boolean;  begin   if (CompareStr(StorageLocation, '') = 0) then StorageLocation := StorageLocationPage.Values[0];   if Port = 0 then Port := StrToInt(PortNumberPage.Values[0]); @@ -149,25 +187,70 @@
  deliberatelyPublic := 1;   end;   + ogStorageLocation := StorageLocation + '\opengrokdata'; + MiniredisDBLocation := StorageLocation + '\miniredis.db';   if not DirExists(StorageLocation) then CreateDir(StorageLocation); + if not DirExists(ogStorageLocation) then CreateDir(ogStorageLocation); + + if IsWin64 then ret := RegQueryStringValue(HKLM64, JAVA_KEY + '\' + JavaVersion, JAVA_HOME, JavaLoc) + else ret := RegQueryStringValue(HKEY_LOCAL_MACHINE, JAVA_KEY + '\' + JavaVersion, JAVA_HOME, JavaLoc); + + JavaLoc := JavaLoc + '\bin\java.exe'     RegWriteStringValue(HKEY_LOCAL_MACHINE, REG_KEY, REPOSITORY_ROOT, StorageLocation);   RegWriteDWordValue(HKEY_LOCAL_MACHINE, REG_KEY, BACKEND_PORT, Port);   RegWriteStringValue(HKEY_LOCAL_MACHINE, REG_KEY, BACKEND_IP, ip);   RegWriteDWordValue(HKEY_LOCAL_MACHINE, REG_KEY, DELIBERATELY_PUBLIC, deliberatelyPublic); + RegWriteStringValue(HKEY_LOCAL_MACHINE, REG_KEY, MINIREDIS_DB, MiniredisDBLocation);   + RegWriteStringValue(HKEY_LOCAL_MACHINE, OG_KEY, JAR, ExpandConstant('{app}\opengrok\opengrok.jar')); + RegWriteStringValue(HKEY_LOCAL_MACHINE, OG_KEY, DATA_DIR, ogStorageLocation); + RegWriteStringValue(HKEY_LOCAL_MACHINE, OG_KEY, CONFIG_UPDATE, 'localhost:2424'); + RegWriteStringValue(HKEY_LOCAL_MACHINE, OG_KEY, JAVA, JavaLoc); + RegWriteStringValue(HKEY_LOCAL_MACHINE, OG_KEY, CTAGS, ExpandConstant('{app}\ctags.exe')); + + RegWriteStringValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, DAEMON_HOST, 'localhost'); + RegWriteDWordValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, DAEMON_PORT, Port + 1); + RegWriteDWordValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, DAEMON_DB, 0); + RegWriteDWordValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, INDEX_THREADS, NINDEX_THREADS); + RegWriteDWordValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, QUEUE_THREADS, NQUEUE_THREADS); + RegWriteStringValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, DAEMON_SSL_KEY, ExpandConstant('{app}\client.key')); + RegWriteStringValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, DAEMON_SSL_CERT, ExpandConstant('{app}\client.crt')); + + if Exec(ExpandConstant('{tmp}\RepoDirectoryMigrator.exe'), '', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) then begin + if ResultCode <> 0 then RaiseException('Failed to migrate repositories to new directory structure!'); + end; + + if Exec(ExpandConstant('{app}\backend.exe'), '--startup auto install', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) then begin + if ResultCode <> 0 then RaiseException('Failed to install service!'); + end;   if Exec(ExpandConstant('{app}\backend.exe'), 'start', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) then begin   if ResultCode <> 0 then RaiseException('Failed to start service!');   end;  end;   +procedure DeinitializeSetup(); +var + BackendPath: String; + ResultCode: Integer; +begin + if StoppedOldService then begin + BackendPath := ExpandConstant('{app}\backend.exe'); + Exec(BackendPath, 'start', '', SW_HIDE, ewNoWait, ResultCode); + end; +end; +  procedure HaltBackend;  var   BackendPath: String;   ResultCode: Integer;  begin   BackendPath := ExpandConstant('{app}\backend.exe'); - if FileExists(BackendPath) then Exec(BackendPath, 'stop', '', SW_HIDE, ewWaitUntilTerminated, ResultCode); + if FileExists(BackendPath) then begin + StoppedOldService := True; + Exec(BackendPath, 'stop', '', SW_HIDE, ewWaitUntilTerminated, ResultCode); + end; + Sleep(3000)  end;    procedure CurStepChanged(CurStep: TSetupStep); @@ -183,5 +266,40 @@
  RegDeleteValue(HKEY_LOCAL_MACHINE, REG_KEY, BACKEND_PORT);   RegDeleteValue(HKEY_LOCAL_MACHINE, REG_KEY, BACKEND_IP);   RegDeleteValue(HKEY_LOCAL_MACHINE, REG_KEY, DELIBERATELY_PUBLIC); + + RegDeleteValue(HKEY_LOCAL_MACHINE, OG_KEY, JAR); + RegDeleteValue(HKEY_LOCAL_MACHINE, OG_KEY, DATA_DIR); + RegDeleteValue(HKEY_LOCAL_MACHINE, OG_KEY, CONFIG_UPDATE); + RegDeleteValue(HKEY_LOCAL_MACHINE, OG_KEY, JAVA); + RegDeleteValue(HKEY_LOCAL_MACHINE, OG_KEY, CTAGS); + + RegDeleteValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, DAEMON_HOST); + RegDeleteValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, DAEMON_PORT); + RegDeleteValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, DAEMON_DB); + RegDeleteValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, INDEX_THREADS); + RegDeleteValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, QUEUE_THREADS); + RegDeleteValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, DAEMON_SSL_KEY); + RegDeleteValue(HKEY_LOCAL_MACHINE, DAEMON_KEY, DAEMON_SSL_CERT);   end;  end; + +function NextButtonClick(CurPageID: Integer) : Boolean; +var + version: String; + ret: Boolean; +begin + if CurPageID = wpWelcome then + begin + if IsWin64 then ret := RegQueryStringValue(HKLM64, JAVA_KEY, JAVA_VERSION, version) + else ret := RegQueryStringValue(HKEY_LOCAL_MACHINE, JAVA_KEY, JAVA_VERSION, version); + + if ret then JavaVersion := version + else + begin + MsgBox('The Kiln Storage Service requires the Java Runtime Environment (JRE) be installed. Please install the JRE for your platform from the Oracle website.', + mbInformation, MB_OK); + Abort(); + end; + end; + Result := True +end;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
@@ -0,0 +1,48 @@
+<Configuration> + <SettingsComponent> + <string /> + <integer /> + <boolean> + <setting name="SolutionAnalysisEnabled">False</setting> + </boolean> + </SettingsComponent> + <RecentFiles> + <RecentFiles> + <File id="AFC5BBEB-4CA4-4AEA-8449-95B66478AC29/f:Program.cs" caret="398" fromTop="14" /> + </RecentFiles> + <RecentEdits> + <File id="AFC5BBEB-4CA4-4AEA-8449-95B66478AC29/f:Program.cs" caret="92" fromTop="3" /> + <File id="AFC5BBEB-4CA4-4AEA-8449-95B66478AC29/f:Program.cs" caret="228" fromTop="9" /> + <File id="AFC5BBEB-4CA4-4AEA-8449-95B66478AC29/f:Program.cs" caret="366" fromTop="14" /> + </RecentEdits> + </RecentFiles> + <NAntValidationSettings> + <NAntPath value="" /> + </NAntValidationSettings> + <UnitTestRunner> + <Providers /> + </UnitTestRunner> + <UnitTestRunnerNUnit> + <NUnitInstallDir IsNull="False"> + </NUnitInstallDir> + <UseAddins>Never</UseAddins> + </UnitTestRunnerNUnit> + <CompletionStatisticsManager> + <ItemStatistics item="Default"> + <Item value="using" priority="0" /> + <Item value="Microsoft" priority="0" /> + <Item value="Win32" priority="0" /> + <Item value="var" priority="2" /> + <Item value="Registry`0" priority="0" /> + <Item value="rk" priority="0" /> + <Item value="const" priority="1" /> + <Item value="string" priority="0" /> + <Item value="RegistryKey`0" priority="0" /> + <Item value="Environment`0" priority="0" /> + <Item value="root" priority="0" /> + </ItemStatistics> + <ItemStatistics item="Qualified:Microsoft.Win32.RegistryKey"> + <Item value="GetValue`0" priority="1" /> + </ItemStatistics> + </CompletionStatisticsManager> +</Configuration> \ No newline at end of file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@@ -0,0 +1,20 @@
+ +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RepoDirectoryMigrator", "RepoDirectoryMigrator\RepoDirectoryMigrator.csproj", "{AFC5BBEB-4CA4-4AEA-8449-95B66478AC29}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AFC5BBEB-4CA4-4AEA-8449-95B66478AC29}.Debug|x86.ActiveCfg = Debug|x86 + {AFC5BBEB-4CA4-4AEA-8449-95B66478AC29}.Debug|x86.Build.0 = Debug|x86 + {AFC5BBEB-4CA4-4AEA-8449-95B66478AC29}.Release|x86.ActiveCfg = Release|x86 + {AFC5BBEB-4CA4-4AEA-8449-95B66478AC29}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@@ -0,0 +1,30 @@
+using System; +using System.IO; +using Microsoft.Win32; + +namespace RepoDirectoryMigrator +{ + class Program + { + static void Main(string[] args) + { + var repoRoot = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Fog Creek Software\Kiln", "KilnRepositoryRoot", null); + if (string.IsNullOrEmpty(repoRoot)) + { + Console.Error.WriteLine("KEY NOT FOUND!"); + Environment.Exit(1); + } + var repositories = Directory.GetDirectories(repoRoot, "????????-????-????-????-????????????"); + foreach (var path in repositories) + { + var repo = Path.GetFileName(path); + var part1 = Path.Combine(repoRoot, repo.Substring(0, 2)); + var part2 = Path.Combine(part1, repo.Substring(2, 2)); + Directory.CreateDirectory(part1); + Directory.CreateDirectory(part2); + Directory.Move(path, Path.Combine(part2, repo)); + } + Console.Error.WriteLine("SUCCESS!"); + } + } +}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@@ -0,0 +1,36 @@
+using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("RepoDirectoryMigrator")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("RepoDirectoryMigrator")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2011")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("47cb10cb-cc59-438e-b866-e7b6eebcbab0")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProductVersion>9.0.30729</ProductVersion> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{AFC5BBEB-4CA4-4AEA-8449-95B66478AC29}</ProjectGuid> + <OutputType>Exe</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>RepoDirectoryMigrator</RootNamespace> + <AssemblyName>RepoDirectoryMigrator</AssemblyName> + <TargetFrameworkVersion>v2.0</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> + <DebugSymbols>true</DebugSymbols> + <OutputPath>bin\x86\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <DebugType>full</DebugType> + <PlatformTarget>x86</PlatformTarget> + <ErrorReport>prompt</ErrorReport> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> + <OutputPath>bin\x86\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <Optimize>true</Optimize> + <DebugType>pdbonly</DebugType> + <PlatformTarget>x86</PlatformTarget> + <ErrorReport>prompt</ErrorReport> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Program.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file
 
 
Change 1 of 1 Show Entire File kiln.wsgi Stacked
 
1
 
 
 
2
3
4
5
6
7
8
9
10
 
11
12
13
14
15
16
17
18
19
20
21
 
 
 
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
 
1
2
3
4
5
6
 
 
 
 
7
8
9
10
11
12
13
14
 
15
 
 
 
 
 
16
17
18
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
@@ -1,43 +1,20 @@
 #!/usr/bin/env python +import site +site.addsitedir('/home/kiln/virtualenv/kiln25/lib/python2.6/site-packages') +  import os  import sys -import urllib -import urllib2 - -from django.core.handlers.wsgi import WSGIHandler    OUR_ROOT = os.path.abspath(os.path.dirname(__file__))  os.environ['HGENCODING'] = 'utf8' +os.environ['TEMP'] = '/home/kiln/data/tmp'  paths = (OUR_ROOT, os.path.join(OUR_ROOT, 'kiln'))  for path in paths:   if path not in sys.path:   sys.path.append(path) - os.environ['DJANGO_SETTINGS_MODULE'] = 'kiln.settings'   -class KilnWSGIHandler(WSGIHandler): - def report_exception(self, e): - def get_stack_trace(): - import traceback - return '\n'.join(traceback.format_exception(*sys.exc_info())) +from kiln.api import handlers +from kiln.versionmiddleware import VersionMiddleware +from kiln.errorloggingmiddleware import ErrorLoggingMiddleware   - bug = {'ScoutUserName': 'BugzScout', - 'ScoutProject': 'Kiln', - 'ScoutArea': 'Backend', - 'Description': str(e), - 'Extra': get_stack_trace()} - - try: - urllib2.urlopen('http://our.fogbugz.com/scoutSubmit.asp', urllib.urlencode(bug)) - except: - pass - - def __call__(self, environ, start_response): - if 'kiln.tempdir' in environ: - os.environ['TMPDIR'] = environ['kiln.tempdir'] - try: - return super(KilnWSGIHandler, self).__call__(environ, start_response) - except Exception, e: - self.report_exception(e) - raise - -application = KilnWSGIHandler() +application = ErrorLoggingMiddleware(VersionMiddleware(handlers.app))
Change 1 of 1 Show Entire File kiln/​__init__.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
@@ -0,0 +1,12 @@
+# legacy imports +from redis.client import Redis, ConnectionPool +from redis.exceptions import RedisError, ConnectionError, AuthenticationError +from redis.exceptions import ResponseError, InvalidResponse, InvalidData + +__version__ = '2.0.0' + +__all__ = [ + 'Redis', 'ConnectionPool', + 'RedisError', 'ConnectionError', 'ResponseError', 'AuthenticationError' + 'InvalidResponse', 'InvalidData', + ]
Change 1 of 1 Show Entire File kiln/​api/​__init__.py Stacked
 
1
 
 
 
 
1
2
@@ -1,1 +1,2 @@
- +import handlers +import queuestats
 
46
47
48
49
 
50
51
52
53
 
 
 
 
 
54
55
56
 
61
62
63
64
 
65
66
67
 
70
71
72
 
73
74
75
76
77
 
81
82
83
 
 
 
84
85
86
 
46
47
48
 
49
50
51
52
 
53
54
55
56
57
58
59
60
 
65
66
67
 
68
69
70
71
 
74
75
76
77
78
 
79
80
81
 
85
86
87
88
89
90
91
92
93
@@ -46,11 +46,15 @@
 import string  import os  import shutil -from mercurial import commands, extensions, util, bdiff +from mercurial import bdiff, commands, extensions, store, util  from mercurial.context import filectx  from mercurial.node import nullrev  from mercurial.i18n import _ -from mercurial.store import hybridencode + +CACHEPATH = 'annotations/' + +def hybridencode(f): + return store._hybridencode(f, lambda path: store._auxencode(path, True))    class annotationcache(object):   ''' Provides access to the cache of file annotations. @@ -61,7 +65,7 @@
  access. A cache file is line-oriented where each line is an   n-tuple of strings separated by the separator character ':'.   - If the file has any ancestor with a different name, then we + If the file has any ancestor with a different name, then we   append .f or .n depending on whether or not we followed the   annotation history to these ancestors. Otherwise a generic   cache is created which works for either case. @@ -70,8 +74,8 @@
  def __init__(self, repo, follow = True):   ''' Create a new annotations cache for the given repository '''   self.followflag = follow and 'f' or 'n' + self._opener = repo.opener   self.cachepath = repo.join("annotations") - self.opener = util.opener(self.cachepath)   self.sepchar = ':'     # fdcache caches information about existing files: @@ -81,6 +85,9 @@
  # fdcache[path] does not exist if the file state is unknown   self.fdcache = {}   + def opener(self, path, *args, **kwargs): + return self._opener(CACHEPATH + path, *args, **kwargs) +   def makepath(self, filectx):   ''' Computes the path to the cache for the given file revision. '''   relpath = os.path.join('data', filectx.path())
Change 1 of 1 Show Entire File kiln/​api/​emptyui.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@@ -0,0 +1,44 @@
+# Copyright (C) 2008-2010 Fog Creek Software. All rights reserved. +# +# This software may be used and distributed according to the terms of the +# GNU General Public License version 2, incorporated herein by reference. + +from mercurial import ui +import traceback +from bugzscout import report_error + +class emptyui(ui.ui): + def __init__(self, src=None, suppressoutput=True): + super(emptyui, self).__init__(src) + if isinstance(src, emptyui): + self.suppressoutput = src.suppressoutput + else: + self.suppressoutput = suppressoutput + + if self.suppressoutput: + self.pushbuffer() + + # Wrap the ui's write functions because writing to stdout causes an exception. + # Save the output using a buffer and create a bug from it later (essentially + # catch the error then report it). + def write_err(self, *args, **opts): + return self.write(*args, **opts) + + def write(self, *args, **opts): + super(emptyui, self).write(*args, **opts) + if self.suppressoutput: + if len(self._buffers) == 1: + super(emptyui, self).write('\n'.join(traceback.format_stack()) + '\n') + + def __del__(self): + if self.suppressoutput: + buffer = self.popbuffer() + if buffer: + report_error('Mercurial output error.', buffer) + try: + super(emptyui, self).__del__() + except AttributeError: + pass + + def readconfig(self, *args, **kwargs): + pass
 
3
4
5
 
 
6
7
8
 
13
14
15
 
16
17
 
 
 
 
 
18
19
20
 
25
26
27
28
 
29
30
 
31
32
 
 
 
33
34
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 
 
 
3
4
5
6
7
8
9
10
 
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
33
34
35
 
36
37
 
38
39
 
40
41
42
43
 
 
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
202
203
204
 
205
206
@@ -3,6 +3,8 @@
 # This software may be used and distributed according to the terms of the  # GNU General Public License version 2, incorporated herein by reference.   +import difflib +import re  from pygments import highlight  from pygments.lexers import get_lexer_for_filename, guess_lexer_for_filename, TextLexer  from pygments.formatters import HtmlFormatter @@ -13,8 +15,14 @@
  'vbs': 'vb',   'fbp5': 'xml',   'xul': 'xml', + 'ipp': 'cpp',   'jsm': 'js'}   +LINE_MAX = 20000 + +def ensurenewline(s): + return s if s.endswith('\n') else s + '\n' +  def tweak(filename):   """change filename to a known extension, if applicable"""   (filename, extension) = filename.split('/')[-1].rsplit('.', 1) @@ -25,50 +33,174 @@
  """select an appropriate lexer based on the filename"""   try:   if content: - return guess_lexer_for_filename(tweak(filename), content, stripnl=False) + l = guess_lexer_for_filename(tweak(filename), content, stripnl=False)   else: - return get_lexer_for_filename(tweak(filename), stripnl=False) + l = get_lexer_for_filename(tweak(filename), stripnl=False)   except: - return TextLexer(stripnl=False) + l = TextLexer(stripnl=False) + l.add_filter('whitespace', spaces=True, wstokentype=False) + return l   -def highlighted(lex, code): - return highlight(code, lex, HtmlFormatter(nowrap=True)) +class IntralineHtmlFormatter(HtmlFormatter): + in_change = False + ranges = [] + + def __init__(self, ranges=None, *args, **kw): + if ranges: + self.ranges = ranges + HtmlFormatter.__init__(self, *args, **kw) + + def _split_change_markers(self, tokensource): + '''Pre-process the token stream before it is formatted, to mark the tokens that should be highlighted for intraline diffs.''' + ranges = self.ranges or [] + pos = 0 + for ttype, value in tokensource: + for value in value.splitlines(True): + l = len(value) + range = None + rr = [r for r in ranges if (r[0] <= pos <= r[1]) or (pos <= r[0] <= r[1] <= pos + l) or (r[0] <= pos + l <= r[1])] + if not rr: + yield ttype, value + pos += l + continue + last = None + for r in rr: + if r[0] <= pos: + # r starts at or before token + if r[1] <= pos + l: + # range covers prefix of token + self.in_change = True + i = r[1] - pos + yield ttype, value[:i] + self.in_change = False + else: + # range covers whole token + self.in_change = True + yield ttype, value + self.in_change = False + else: + # r starts in the middle of the token + i = last[1] - pos if last else 0 + j = r[0] - pos + yield ttype, value[i:j] + if r[1] <= pos + l: + # range covers middle chunk + self.in_change = True + i = r[0] - pos + j = r[1] - pos + yield ttype, value[i:j] + self.in_change = False + else: + # range covers suffix of token + self.in_change = True + i = r[0] - pos + yield ttype, value[i:] + self.in_change = False + last = r + if last[1] <= pos + l: + i = last[1] - pos + yield ttype, value[i:] + pos += l + + def _format_lines(self, tokensource): + return super(IntralineHtmlFormatter, self)._format_lines(self._split_change_markers(tokensource)) + + def _get_css_class(self, ttype): + return super(IntralineHtmlFormatter, self)._get_css_class(ttype) + (' ch' if self.in_change else '') + +def highlighted(lex, code, ranges=None): + return highlight(code, lex, IntralineHtmlFormatter(ranges, nowrap=True)) + +def highlight_patch(lex, lines, ranges=None): + lines = [(line[0], ensurenewline(line[1:LINE_MAX])) for line in lines] + for x in xrange(0, len(lines)): + if lines[x][0] == '\\': + lines[x] = (lines[x][0], '\n') + patch = ''.join(l[1] for l in lines) + patch = highlighted(lex, patch, ranges).splitlines(True) + for x in xrange(0, min(len(patch), len(lines))): + if lines[x][0] == '\\': + lines[x] = (lines[x][0], ' No newline at end of file\n') + else: + lines[x] = (lines[x][0], patch[x]) + return ''.join(line[0] + line[1] for line in lines) + +# returns a list of ranges (a, b), marking that characters a:b in the patch are changed. +def intraline_diff(patch): + removed_lines = [] + added_lines = [] + ranges = [] + l = 0 + + for line in patch + [' ']: + if line[0] == '-': + removed_lines.append(line[1:]) + elif line[0] == '+': + added_lines.append(line[1:]) + else: + if added_lines or removed_lines: + rtotal = sum(len(s) for s in removed_lines) + atotal = sum(len(s) for s in added_lines) + + # split the diff text into whole words and individual non-word characters + removed_words = [w for w in re.split(r'(\w+|\W)', ''.join(removed_lines)) if w] + added_words = [w for w in re.split(r'(\w+|\W)', ''.join(added_lines)) if w] + removed, added = l, l + rtotal + seq = difflib.SequenceMatcher(); + seq.set_seqs(removed_words, added_words) + + # find the matching words of each string, using the ranges in each opcode. + # 'equal' action is for non-changed text; otherwise, mark the range as changed. + for (action, r1, r2, a1, a2) in seq.get_opcodes(): + ac = ''.join(added_words[a1:a2]) + rc = ''.join(removed_words[r1:r2]) + a = len(ac) + r = len(rc) + added += a + removed += r + if action == 'equal': + continue + if a != 0: + ranges.append((added - a, added)) + if r != 0: + ranges.append((removed - r, removed)) + + l += atotal + rtotal + removed_lines = [] + added_lines = [] + l += len(line) - 1 + return sorted(ranges) + +def format(filename, diff): + if not diff: + return None + formatted = [] + patch = [] + + if not isinstance(diff, unicode): + diff_asc = diff + else: + diff_asc = diff.encode('utf-8') + diff_asc = diff_asc.replace('\r', '') + lines = diff_asc.splitlines(True) + if isinstance(diff, unicode): + lines = [l.decode('utf-8') for l in lines] + + lex = lexer(filename) + for line in lines: + if line.startswith(u'@@'): + if patch: formatted.extend(highlight_patch(lex, patch, intraline_diff(patch))) + formatted.append(line) + patch = [] + else: + patch.append(line) + if patch: formatted.extend(highlight_patch(lex, patch, intraline_diff(patch))) + return ''.join(formatted)    def format_diffs(diffs): - def highlight_patch(lex, lines): - lines = [(line[0], line[1:]) for line in lines] - for x in xrange(0, len(lines)): - if lines[x][0] == '\\': - lines[x] = (lines[x][0], '\n') - patch = ''.join(l[1] for l in lines) - patch = highlighted(lex, patch).splitlines(True) - for x in xrange(0, min(len(patch), len(lines))): - if lines[x][0] == '\\': - lines[x] = (lines[x][0], ' No newline at end of file\n') - else: - lines[x] = (lines[x][0], patch[x]) - return ''.join(line[0] + line[1] for line in lines) - - def format(filename, diff): - if not diff: - return None - formatted = [] - patch = [] - diff = diff.replace('\r', '') - lines = diff.splitlines(True) - lex = lexer(filename) - for line in lines: - if line.startswith('@@'): - if patch: formatted.extend(highlight_patch(lex, patch)) - formatted.append(line) - patch = [] - else: - patch.append(line) - if patch: formatted.extend(highlight_patch(lex, patch)) - return ''.join(formatted) -   for d in diffs:   d['formatted_diff'] = format(d['file']['name'], d['diff'])    def format_file(filename, contents): - return highlighted(lexer(filename), contents.replace('\r', '')) + lines = [line[:LINE_MAX] for line in contents.replace('\r', '').split('\n')] + return highlighted(lexer(filename), '\n'.join(lines))
 
1
 
2
3
4
5
 
 
 
 
6
7
8
9
10
11
12
 
 
 
 
 
 
13
14
15
16
17
18
19
 
 
20
 
 
 
 
 
 
 
21
22
23
24
25
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
30
31
32
33
34
35
 
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
 
 
 
142
143
144
145
146
 
 
 
 
 
 
 
 
 
 
 
 
 
147
148
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
153
154
155
156
157
 
 
 
 
 
158
159
160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
162
163
164
165
166
167
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
192
193
194
195
196
197
 
 
 
 
 
 
 
 
198
199
200
 
 
201
202
 
 
 
 
 
 
 
 
 
 
 
203
204
205
 
207
208
209
210
 
211
212
213
214
215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
217
 
218
219
220
 
 
 
 
 
221
222
223
224
225
226
227
228
229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
231
232
233
 
 
 
 
 
 
 
 
 
 
 
 
234
235
 
 
 
236
237
238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
 
 
 
 
 
 
11
12
13
14
15
16
17
 
 
 
 
 
 
18
19
20
21
22
23
24
25
26
27
28
 
 
 
29
 
 
 
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 
 
 
61
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
64
65
66
67
 
 
 
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 
 
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
 
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
 
 
 
 
284
285
286
287
288
289
 
 
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
 
 
 
 
 
 
 
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
 
 
 
 
 
 
351
352
353
354
355
356
357
358
359
360
 
361
362
363
 
364
365
366
367
368
369
370
371
372
373
374
375
376
377
 
379
380
381
 
382
383
 
 
 
 
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
 
630
631
 
 
632
633
634
635
636
637
 
 
 
 
 
 
 
 
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
 
 
 
660
661
662
663
664
665
666
667
668
669
670
671
672
 
673
674
675
676
 
 
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
697
698
699
700
701
702
703
704
705
@@ -1,205 +1,377 @@
-# Copyright (C) 2009-2010 by Fog Creek Software. All rights reserved. +# Copyright (C) 2009-2011 by Fog Creek Software. All rights reserved.  #  # This software may be used and distributed according to the terms of the  # GNU General Public License version 2, incorporated herein by reference.   +from functools import wraps +import hashlib +import os +import urllib2   -import base64 -import os -import sys -import urllib -import urllib2 -from threading import Thread +from flask import Flask, Response, request +from mercurial import hgweb, util, context +from mercurial.error import LockHeld, RepoLookupError +from werkzeug.exceptions import NotFound, BadRequest +import settings +import simplejson   -from django.conf import settings -from django.utils import simplejson -from mercurial import ui, util -from piston.emitters import Emitter -from piston.handler import AnonymousBaseHandler, typemapper -from piston.utils import rc +import Image +import cStringIO   +from bugzscout import report_exception +from encoders import EmittableEncoder +from formatter import format_diffs, format_file +from repositories import Repository, RepositoryNotSubsetException, CreatesNewHeadsException, filetuple, hexdecode, determinedisplaysize +from webtasks import asyncpost, queue_repo_index, queue_repo_create, queue_repo_strip +import bfiles +import syncstatus  import urlutil -from formatter import format_diffs, format_file -from repositories import Repository, RepositoryNotSubsetException, CreatesNewHeadsException -from repositories import filetuple, hexdecode   -class fakerequest(object): - pass -fakerequest.GET = {} +app = Flask(__name__) + +def jsonify(obj): + if isinstance(obj, Response) or isinstance(obj, basestring): + return obj + return Response(enc.encode(obj), mimetype='application/json') + +def route(url, methods=['GET'], as_json=True): + def wrapper(f): + @app.route(url, methods=methods) + @wraps(f) + def inner(*args, **kwargs): + r = f(*args, **kwargs) + if as_json: + r = jsonify(r) + return r + return inner + return wrapper + +def get(url, as_json=True): + return route(url, methods=['GET'], as_json=as_json) + +def post(url, as_json=True): + return route(url, methods=['POST'], as_json=as_json) + +def delete(url): + return app.route(url, methods=['DELETE'])    def error(message, code):   return {'type': 'error', 'message': message, 'code': code}   -def reportexception(e): - if settings.DEBUG: - return +enc = EmittableEncoder()   - def gettraceback(): - import traceback - return '\n'.join(traceback.format_exception(*(sys.exc_info()))) - - traceback = gettraceback() - bug = {'ScoutUserName': settings.FOGBUGZ_USERNAME, - 'ScoutProject': settings.FOGBUGZ_PROJECT, - 'ScoutArea': settings.FOGBUGZ_AREA, - 'Description': 'Backend exception: %s' % e, - 'Extra': traceback} - - if settings.HOSTED: - try: - urllib2.urlopen(settings.FOGBUGZ_URL, urllib.urlencode(bug)) - except: - pass - else: - from filelogmiddleware import _log_error - _log_error(bug) - -class PingbackThread(Thread): - def __init__(self, handler, method, pingback, request, args, kwargs): - super(PingbackThread, self).__init__() - self.handler = handler - self.method = method - self.pingback = pingback - self.request = request - self.args = args - self.kwargs = kwargs - - def run(self): - r = self.method(self.handler, *self.args, **self.kwargs) - emitter, mime = Emitter.get('json') - srl = emitter(r, typemapper, self.handler, self.handler.fields, True) - json = srl.render(fakerequest) - - success = False - attempts = 3 - while attempts and not success: - try: - attempts -= 1 - urllib2.urlopen(self.pingback, urllib.urlencode({'data': json.encode('utf8')})) - success = True - except urllib2.URLError, e: - if attempts == 0: - reportexception(e) - -def ping_wrapper(method): - def f(self, *args, **kwargs): - q = args[0].POST - if 'pingback' in q: - t = PingbackThread(self, method, q['pingback'], args[0], args, kwargs) - try: - t.start() - except Exception, e: - print e - return rc.ALL_OK - else: - return method(self, *args, **kwargs) - return f - -def with_pingbacks(cls): - """pingback wrapper - - This is a decorator that makes any given handler function run - asynchronously if provided with a pingback parameter in the - web request.""" - - for m in ('create', 'read', 'delete', 'update'): - if m in cls.__dict__.keys(): - method = getattr(cls, m) - method = ping_wrapper(method) - setattr(cls, m, method) - return cls - -@with_pingbacks -class RepositoryHandler(AnonymousBaseHandler): - allowed_methods = ('GET', 'POST', 'DELETE',) - model = Repository - fields = ('uuid', 'parent',) - - def read(self, request, uuid=None): - if uuid: - r = Repository(uuid) - if r.exists(): - return r - return rc.NOT_FOUND - if not settings.HOSTED: - return rc.BAD_REQUEST - return [Repository(folder) - for folder in os.listdir(settings.KILN_REPOSITORY_ROOT) - if Repository(folder).exists()] - - def create(self, request): - q = request.POST - uuid = q['uuid'] - meta = simplejson.loads(q['meta']) if q.get('meta') else {} - if q.get('parent'): - r = Repository(q['parent']).cloneto(uuid, meta) - else: - r = Repository(uuid) - r.create(meta) - return r - - def delete(self, request, uuid): +@get('/repo/<uuid>') +def repo_get(uuid=None): + if uuid:   r = Repository(uuid)   if r.exists(): - r.delete() - return rc.DELETED - return rc.NOT_FOUND + return r + raise NotFound + if not settings.HOSTED: + raise BadRequest + repos = [Repository(folder) + for folder in os.listdir(settings.KILN_REPOSITORY_ROOT) + if Repository(folder).exists()] + for p1 in os.listdir(settings.KILN_REPOSITORY_ROOT): + if len(p1) == 2: + for p2 in os.listdir(os.path.join(settings.KILN_REPOSITORY_ROOT, p1)): + parent = os.path.join(settings.KILN_REPOSITORY_ROOT, p1, p2) + repos.extend(Repository(folder) for folder in os.listdir(parent) if Repository(folder).exists()) + return repos   -class ManifestHandler(AnonymousBaseHandler): - allowed_methods = ('GET',) +@post('/repo') +def repo_create(): + q = request.form + try: + uuid = q['uuid'] + pingback = q['pingback'] + site = urlutil.siteurl(request) + meta = q.get('meta', None) + parent = q.get('parent', None) + except Exception, e: + raise + return BadRequest(e) + queue_repo_create(uuid, pingback, site, meta=meta, parent=parent) + return 'OK'   - def read(self, request, uuid, rev='tip'): +@post('/repo/<uuid>') +def update_meta(uuid): + q = request.form + try: + meta = simplejson.loads(q['meta']) if q.get('meta') else {} + except: + raise BadRequest + + r = Repository(uuid) + if not r.exists(): + raise NotFound + r.meta = meta + return r + +@delete('/repo/<uuid>') +def repo_delete(uuid): + # This can only ever be called manually, so it's okay that + # this key is never used on the website side. If we do ever + # add repository purging via heartbeat or whatever, this + # will obviously need to change + if settings.HOSTED: + if request.args.get('magic_word') != settings.WHITE_RABBIT_OBJECT: + raise BadRequest + r = Repository(uuid) + if r.exists(): + r.delete() + syncstatus.remove_repo(r) + return Response('', status=204) + raise NotFound + +@post('/repo/<uuid>/commit') +def commit(uuid): + q = request.form + author = q['author'] + parent = q['parent'] + date = q['date'] + message = q['message'] + path = hexdecode(q['path']) + upload = request.files['file'] + if upload.content_length > settings.KILN_MAX_COMMIT_FILE_SIZE: + return error('The uploaded file is too large.', 'too_large') + data = upload.read() + if hasattr(upload, 'close'): + upload.close() + + def _writefile(repo, mctx, path): + return context.memfilectx(path, data) + + r = Repository(uuid) + if not r.exists(): + raise NotFound + + repo = r.repo + l = None + try: + l = repo.lock() + except LockHeld: + if l: l.release() + return error('The repository is locked.', 'repo_locked') + try: + try: + ctx = repo[parent] + if ctx.children(): + return error('Commit creates new head!', 'not_head') + except RepoLookupError: + raise NotFound + mctx = context.memctx(repo, [parent, None], message, [path], _writefile, user=author, date=date) + mctx.commit() + except Exception, e: + report_exception(e) + raise + finally: + if l: l.release() + + return Response('OK') + +@post('/repo/stripped') +def strip(): + q = request.form + uuid = q['uuid'] + parent = q['parent'] + pingback = q['pingback'] + rev = q['rev'] + url = q['url'] + ixPerson = q['ixperson'] + meta = q.get('meta', '') + parent = q['parent'] + if not Repository(parent).exists(): + raise NotFound + queue_repo_strip(pingback, uuid, parent, rev, meta, url, ixPerson) + return Response('OK') + +@get('/repo/<uuid>/manifest/<rev>') +def manifest(uuid, rev='tip'): + r = Repository(uuid) + if not r.exists(): + raise NotFound + if not r.hasrevision(rev): + raise BadRequest + return {'type': 'manifest', 'manifest': r.manifest(rev)} + +@get('/repo/<uuid>/size') +def size(uuid): + r = Repository(uuid) + if not r.exists(): + # raise NotFound + # Hack around a dumb bug in ourdot's Kiln install + return {'type': 'reposize', 'size': 0} + return {'type': 'reposize', 'size': r.size()} + +@get('/repo/<uuid>/commontag') +def common_tags(uuid): + """ + This function takes a list of checkins within a repository and + will return the nearest common child which has a tag. + """ + r = Repository(uuid) + if not 'revs' in request.args or not r.exists(): + raise BadRequest + else: + revs = request.args['revs'].split(","); + + if not 'num_tags' in request.args: + num_tags = 1 + else: + num_tags = int(request.args['num_tags']) + tags = r.commontags(revs, num_tags); + + return {'type': 'tags', 'tags': tags}; + +@post('/repo/<uuid>/tag/<rev>') +def create_tag(uuid, rev='tip'): + r = Repository(uuid) + if not r.exists(): + raise NotFound + try: + tag = request.form['tag'] + ixPerson = request.form['ixPerson'] + url = request.form['url'] + username = request.form['username'] + except KeyError: + raise BadRequest + force = False + if 'force' in request.form and request.form['force'].lower() != 'false': + force = True + try: + r.tag(rev, tag, url, ixPerson, username, force) + except ValueError: + raise BadRequest + + return {'type': 'tag', 'tag': tag, 'rev': rev} + +@get('/repo/<uuid>/tag') +def get_tags(uuid): + r = Repository(uuid) + if not r.exists(): + raise NotFound + return {'type': 'tags', 'tags': r.tags()} + +@get('/repo/<uuid>/changesbetweentags') +def betweentags(uuid): + r = Repository(uuid) + if not r.exists(): + raise NotFound + try: + tag1 = request.args["tag1"] + tag2 = request.args["tag2"] + except KeyError: + raise BadRequest + + try: + changesetlist = r.changesbetweentags(tag1, tag2, request.args.get('includelow', 'false').lower() == 'true') + return {'type': 'changesets', 'changesets': changesetlist} + except: + raise BadRequest + +@post('/repo/meta') +def set_meta(): + '''Takes a JSON dictionary of repo uuid => repo metadata, at the key + 'meta', and updates the metadata for those repos. Returns a dictionary + of uuid => boolean, with True for repos that were found and False for + repos that do not exist.''' + exists = {} + meta = simplejson.loads(request.form['meta']) + for uuid in meta:   r = Repository(uuid)   if not r.exists(): - return rc.NOT_FOUND - if not r.hasrevision(rev): - return rc.BAD_REQUEST - return {'type': 'manifest', 'manifest': r.manifest(rev)} + exists[uuid] = False + continue + exists[uuid] = True + r.meta = simplejson.loads(meta[uuid]) + return exists   -class SizeHandler(AnonymousBaseHandler): - allowed_methods = ('GET',) +@get('/repo/<uuid>/file/<rev>/') +@get('/repo/<uuid>/file/<rev>/<path:path>') +def get_file(uuid, path='', rev='tip'): + r = Repository(uuid) + binaries = int(request.args.get('binaries', 0)) + images = int(request.args.get('images', 0)) + can_truncate = not int(request.args.get('no_truncate', 0)) + no_contents = int(request.args.get('no_contents', 0)) + path = hexdecode(path) + if not r.exists(): + raise NotFound + if not r.hasrevision(rev): + raise BadRequest + if r.hasfile(path, rev): + return filecontents(r, path, rev, binaries, images, can_truncate, no_contents) + else: + return directorylisting(r, path, rev)   - def read(self, request, uuid): - r = Repository(uuid) - if not r.exists(): - # return rc.NOT_FOUND - # Hack around a dumb bug in ourdot's Kiln install - return {'type': 'reposize', 'size': 0} - return {'type': 'reposize', 'size': r.size()} +@get('/repo/<uuid>/file/<rev1>/<rev2>/<path:path>') +def get_subtracted_image(uuid, path='', rev1='tip', rev2='tip'): + r = Repository(uuid) + path = hexdecode(path) + if not r.exists(): + raise NotFound + if not r.hasrevision(rev1) or not r.hasrevision(rev2): + raise BadRequest + if r.hasfile(path, rev1) and r.hasfile(path, rev2): + #open the old and new versions of the image in RGB mode, and resize them so that the largest dimension is 300px. + oldcontents = Image.open(cStringIO.StringIO(r.filecontents(path, rev1, raw=1))) + oldcontents = resizeimage(oldcontents, displaySize=tuple(determinedisplaysize(oldcontents.size, max=(500, 500)))).convert("RGB") + newcontents = resizeimage(Image.open(cStringIO.StringIO(r.filecontents(path, rev2, raw=1))), displaySize=tuple(determinedisplaysize(oldcontents.size, max=(500, 500)))).convert("RGB") + sub = subtractimages(oldcontents, newcontents) + + #im = Image.new("RGB", (oldcontents.size[0]*3, oldcontents.size[1])) + #im.paste(oldcontents, (0,0, sub.size[0], sub.size[1])) + #im.paste(sub, (sub.size[0],0, sub.size[0]*2, sub.size[1])) + #im.paste(newcontents, (sub.size[0]*2,0,sub.size[0]*3,sub.size[1])) + im = sub + + output = cStringIO.StringIO() + im.save(output, "PNG") + return Response(output.getvalue())   -class FileHandler(AnonymousBaseHandler): - allowed_methods = ('GET',) - - def read(self, request, uuid, path, rev='tip'): - r = Repository(uuid) - binaries = int(request.GET.get('binaries', 0)) - can_truncate = not int(request.GET.get('no_truncate', 0)) - path = hexdecode(path) - if not r.exists(): - return rc.NOT_FOUND - if not r.hasrevision(rev): - return rc.BAD_REQUEST - if r.hasfile(path, rev): - return self.filecontents(r, path, rev, binaries, can_truncate) - else: - return self.directorylisting(r, path, rev) - - def filecontents(self, repo, path, rev, binaries, can_truncate): - truncated = False - contents = repo.filecontents(path, rev) - ft = filetuple(path) +def filecontents(repo, path, rev, binaries, images, can_truncate, no_contents): + truncated = False + ft = filetuple(path) + if repo.isbfile(path) and not binaries: + try: + Image.open(cStringIO.StringIO(repo.filecontents(path, rev, raw=True))) + filetype = 'image' + contents = '(Image file)' + except IOError: + filetype = 'binary' + contents = '(Binary file)' + elif no_contents: + contents = '' + truncated = True + filetype = 'text' + else: + contents = repo.filecontents(path, rev, raw=binaries)   if util.binary(contents): - if binaries: - filetype = "base64" - contents = base64.b64encode(contents) - else: - filetype = 'binary' - contents = '(Binary file)' + if not binaries: + try: + Image.open(cStringIO.StringIO(repo.filecontents(path, rev, raw=True))) + filetype = 'image' + contents = '(Image file)' + except IOError: + filetype = 'binary' + contents = '(Binary file)'   else:   filetype = 'text' - if len(contents) > 300000 and can_truncate: + truncate_length = 200000 + if len(contents) > truncate_length and can_truncate:   truncated = True - contents = contents[:300000] + contents = contents[:truncate_length] + if binaries: + if images: + try: + imfile = cStringIO.StringIO() + resizeimage(Image.open(cStringIO.StringIO(contents))).save(imfile, "PNG") + contents = imfile.getvalue() + except IOError: + pass + return Response(contents) + else:   return {'type': 'file',   'path': ft['path'],   'bytepath': ft['bytepath'], @@ -207,209 +379,327 @@
  'filetype': filetype,   'truncated': truncated,   'contents': contents, - 'formatted_contents': format_file(path, contents) if can_truncate else None} + 'formatted_contents': format_file(path, contents) if not truncated else None}   - def directorylisting(self, repo, path, rev): - files = repo.directorylisting(path, rev) - if files == None: - return rc.NOT_FOUND +def resizeimage(image, displaySize=None): + if displaySize == None: + displaySize = tuple(determinedisplaysize(image.size)) + if image.size == displaySize: + return image + else: + return image.resize(displaySize) + +def subtractimages(oldimage, newimage): + im = Image.new("RGB", oldimage.size) + pix = im.load() + npix = newimage.load() + opix = oldimage.load() + for x in xrange(oldimage.size[0]): + for y in xrange(newimage.size[1]): + pix[x, y] = abs(npix[x, y][0] - opix[x, y][0]), abs(npix[x, y][1] - opix[x, y][1]), abs(npix[x, y][2] - opix[x, y][2]) + pix[x, y] = leahhighlight(pix[x,y]) + return im + +def andrewdifference(pix): + pix = f(pix[0]),f(pix[1]),f(pix[2]) + return pix + +def f(x): + return int(256*pow((float(x)/256),.5)) + +def leahhighlight(pix): + if pix[0] >= 18 and pix[1] >= 18 and pix[2] >= 18: + pix = 5* pix[0],10* pix[1],5* pix[2] + return pix + +def directorylisting(repo, path, rev): + files = repo.directorylisting(path, rev) + if files == None: + raise NotFound + else: + return {'type': 'files', 'files': files} + +@get('/repo/<uuid>/annotate/<rev>/<path:path>') +def annotate(uuid, path, rev): + r = Repository(uuid) + path = hexdecode(path) + if not r.exists() or not r.hasfile(path, rev): + raise NotFound + contents = r.filecontents(path, rev) + if util.binary(contents): + return error('Unable to annotate binary files', 'annotate_binary') + + if request.args.get('line'): + return linehistory(r, path, rev, + int(request.args['line']), int(request.args.get('count', 4))) + else: + return filehistory(r, path, rev, int(request.args.get('count', 0))) + +def linehistory(r, path, rev, line, count): + return {'type': 'changesets', 'changesets': r.annotateline(path, rev, line, count)} + +def filehistory(r, path, rev, count): + return {'type': 'annotation', 'annotation': r.annotate(path, rev, count=count)} + +@get('/repo/<uuid>/branches') +def branches(uuid): + r = Repository(uuid) + if not r.exists(): + raise NotFound + return r.branches() + +@post('/repo/<uuid>/changeset') # For many changesets, e.g. reviews. +@get('/repo/<uuid>/changeset/<revs>') +@get('/repo/<uuid>/changeset/<revs>/<filename>') +def changesets(uuid, revs=None, filename=None): + r = Repository(uuid) + if not r.exists(): + raise NotFound + + if request.method == 'POST': + revs = request.form['revs'] + filename = request.form.get('filename', None) + + if filename: + filename = hexdecode(filename) + + changedfiles = request.values.get('changedfiles') + + revs = revs.split(':') + try: + if len(revs) == 1: + # did you instead give us an enumeration of individual changesets? + revs = revs[0].split(',') + if len(revs) == 1: + # Only one changeset, allow for file changesets + if filename: + return dict(r.filechangeset(filename, revs[0]), type='filechangeset') + else: + return dict(r.changeset(revs[0], changedfiles), type='changeset') + else: + # multiple changesets + return {'type': 'changesets', + 'changesets': r.changesets(revs, changedfiles)} + elif len(revs) == 2: + if filename: + limit = int(request.values.get('limit', 0)) + return {'type': 'filechangesets', + 'filechangesets': r.filechangesets(filename, revs[0], revs[1], limit)} + else: + return {'type': 'changesets', + 'changesets': r.changesetrange(revs[0], revs[1], changedfiles)} + except: + raise BadRequest + +@get('/repo/<uuid>/diff/<revs>') +@get('/repo/<uuid>/diff/<revs>/<filename>') +def diff(uuid, revs, filename=None): + r = Repository(uuid) + if filename: + filename = hexdecode(filename) + + if not r.exists(): + raise NotFound + revs = revs.split(':') + for rev in revs: + if not r.hasrevision(rev): + raise BadRequest + + # Set maxsize to 80 kB or as requested, unless it's a single file, + # in which case serve 200kb. The value of 80 kB was + # lovingly determined by trial and error. If you change it, + # please remember at least to do the former. + maxsize = int(request.args.get('maxsize') or (200 if filename else 80) * 1000) + + ignorews = request.args.get('ignorews', 'False').lower() == 'true' + + opts = dict(filename=filename, maxsize=maxsize, ignorews=ignorews) + if len(revs) > 1: + opts['rev2'] = revs[1] + udiff, bytecount = r.diff(revs[0], **opts) + format_diffs(udiff) + + if filename: + if udiff: + return udiff[0] + return {'type': 'diff'} + else: + return {'type': 'diffs', + 'truncated': bytecount - maxsize > 0, + 'diffs': udiff} + +@get('/repo/<uuid>/outgoing/<uuid2>') +def outgoing_get(uuid, uuid2): + r1 = Repository(uuid) + r2 = Repository(uuid2) + nochangesets = int(request.args.get('nochangesets', 0)) + if not r1.exists(): + raise NotFound + if not r2.exists(): + raise BadRequest + if not r1.isrelated(r2): + return error('repositories are not related', 'notrelated') + return {'type': 'outgoing', 'newheads': r1.pushwouldmakeheads(r2), 'changesets': [] if nochangesets else r1.outgoing(r2)} + +@post('/repo/<uuid>/outgoing/<uuid2>') +def push_repo(uuid, uuid2): + r1 = Repository(uuid) + r2 = Repository(uuid2) + ixPerson = request.form['ixPerson'] + url = request.form['website'] + if not r1.exists(): + raise NotFound + if not r2.exists(): + raise BadRequest + if not r1.isrelated(r2): + return error('repositories are not related', 'notrelated') + if not r1.outgoing(r2): + return error("repositories were already sync'd", 'alreadysyncd') + try: + return {'type': 'push', 'success': r1.push(r2, url, pusher=ixPerson)} + except RepositoryNotSubsetException, e: + return error(str(e), 'notstrictsubset') + except CreatesNewHeadsException, e: + return error(str(e), 'newheads') + +@post('/sync') +def sync(): + if not settings.HOSTED: + raise BadRequest + remote = request.form["remote"] + if 'repo' not in request.form: + # We don't have a specific repo, so we'll trigger a sync to every repo that needs it. + repos = syncstatus.need_sync(remote) + for repo in repos: + asyncpost(request.base_url, dict(remote=remote, repo=repo)) + return dict(type='sync', success=True, count=len(repos)) + resp = urllib2.urlopen(urlutil.urljoin(remote, "repo/%s" % request.form['repo'])) + repo = simplejson.loads(resp.read()) + failures = [] + relink = False + r = Repository(repo['uuid'], suppresshooks=True) + if not r.exists(): + r.create(repo['meta']) + relink = True + r.meta = repo['meta'] + if 'bfile' in request.form: + try: + sha = request.form['bfile'] + if bfiles.ishash(sha) and not bfiles.instore(sha): + resp = urllib2.urlopen(urlutil.urljoin(remote, 'repo', r.uuid, 'bfile', sha)) + bfiles.storebfile(resp, sha) + except Exception, e: + failures.append({'repo': repo['uuid'], 'exception': e}) + report_exception(e) + else: + remoteurl = urlutil.urljoin(remote, 'repo', r.uuid) + try: + r.pull(remoteurl) + if settings.DO_INDEXING: + queue_repo_index(repo['uuid']) + if settings.HOSTED: + syncstatus.update_status(r) + if relink: + r.relink() + # Chain the sync along + r.sync(site=urlutil.siteurl(request), peers=dict(r.ui.configitems('post_peers'))) + except LockHeld, e: + # No need to report locked repos. They're expected. + failures.append({'repo': repo['uuid'], 'exception': e}) + except Exception, e: + failures.append({'repo': repo['uuid'], 'exception': e}) + report_exception(e, "uuid=%s, r.repo['tip'].rev()=%s, request.form=%s\n" + % (repo['uuid'], str(r.repo['tip'].rev()), str(request.form))) + d = {'type': 'sync', 'success': not failures} + if failures: + d['failures'] = failures + return d + +@get('/version') +def version(): + return {'version': settings.KILN_BACKEND_VERSION, 'hg_version': util.version()} + +@app.route('/repo/<uuid>/bfile', methods=['GET', 'POST']) +@app.route('/repo/<uuid>/bfile/<sha>', methods=['GET', 'POST']) +def bfilehandle(uuid, sha=None): + repo = Repository(uuid) + if not sha: + if request.method == 'GET': + return Response(simplejson.dumps(bfiles.listbfiles()))   else: - return {'type': 'files', 'files': files} + raise BadRequest   -class AnnotationHandler(AnonymousBaseHandler): - allowed_methods = ('GET',) + if request.method == 'GET': + try: + return Response(bfiles.bfilecontents(sha)) + except IOError: + raise NotFound   - def read(self, request, uuid, path, rev): - r = Repository(uuid) - path = hexdecode(path) - if not r.exists() or not r.hasfile(path, rev): - return rc.NOT_FOUND - contents = r.filecontents(path, rev) - if util.binary(contents): - return error('Unable to annotate binary files', 'annotate_binary') + # bfiles uses PUT to upload files but django read the entire file into memory + # use POST instead so that we can access the file with a generator + # NOTE: This may no longer be necessary with flask, but it's the way it works + # so there's no reason to change it back right now. + elif request.method == 'POST': + try: + if bfiles.instore(sha): + return Response(status=200) + elif bfiles.storebfile(request.files['name'], sha): + try: + repo.sync(site=urlutil.siteurl(request), + bfile=sha, + peers=dict(repo.ui.configitems('peers'))) + finally: + return Response(status=201) + else: + #SHA1 is checked by storebfile + raise BadRequest('SHA1 of file does not match SHA1 given.') + except Exception, e: + report_exception(e) + raise BadRequest   - if request.GET.get('line'): - return self.linehistory(r, path, rev, - int(request.GET['line']), int(request.GET.get('count', 4))) + elif request.method == 'HEAD': + if bfiles.instore(sha): + m = hashlib.sha1() + with bfiles.bfilecontents(sha) as fd: + while True: + data = fd.read(32768) + if not data: + break + m.update(data) + response = Response() + response.headers['Content-SHA1'] = m.hexdigest() + return response   else: - return self.filehistory(r, path, rev) + raise NotFound + else: + raise BadRequest   - def linehistory(self, r, path, rev, line, count): - return {'type': 'changesets', 'changesets': r.annotateline(path, rev, line, count)} +@app.route('/repo/<uuid>/serve', methods=['GET', 'POST']) +def serve(uuid): + r = Repository(uuid, suppressoutput=False) + if not r.exists(): + raise NotFound + repo = r.repo + if 'ixPerson' in request.args: + repo.ui.setconfig('kiln', 'ixperson', request.args['ixPerson']) + repo.ui.setconfig('kiln', 'url', request.args['website']) + repo.ui.setconfig('kiln', 'site', urlutil.siteurl(request)) + repo.ui.setconfig('kiln', 'token', request.args.get('token', '')) + # if we're about to push, run recover. Don't do this for pull, + # because it locks the repo (even if only for a second), and it's + # obviously better if we don't have to wait for a push to finish + # to pull + if request.args['cmd'] == 'unbundle': + r.recover() + request.environ['REPO_NAME'] = request.environ['PATH_INFO'].strip('/') + return hgweb.hgweb(repo.root, baseui=repo.ui)   - def filehistory(self, r, path, rev): - return {'type': 'annotation', 'annotation': r.annotate(path, rev)} - -class ChangesetHandler(AnonymousBaseHandler): - allowed_methods = ('GET', 'POST') - - def create(self, request, uuid): - revs = request.POST["revs"] - filename = request.POST.get("filename") - return self.read(request, uuid, revs, filename) - - def read(self, request, uuid, revs, filename=None): - r = Repository(uuid) - if filename: - filename = hexdecode(filename) - - if not r.exists(): - return rc.NOT_FOUND - - changedfiles = request.REQUEST.get('changedfiles') - - revs = revs.split(':') - try: - if len(revs) == 1: - # did you instead give us an enumeration of individual changesets? - revs = revs[0].split(',') - if len(revs) == 1: - # Only one changeset, allow for file changesets - if filename: - return dict(r.filechangeset(filename, revs[0]), type='filechangeset') - else: - return dict(r.changeset(revs[0], changedfiles), type='changeset') - else: - # multiple changesets - return {'type': 'changesets', - 'changesets': r.changesets(revs, changedfiles)} - elif len(revs) == 2: - if filename: - limit = int(request.REQUEST.get('limit', 0)) - return {'type': 'filechangesets', - 'filechangesets': r.filechangesets(filename, revs[0], revs[1], limit)} - else: - return {'type': 'changesets', - 'changesets': r.changesetrange(revs[0], revs[1], changedfiles)} - except: - return rc.BAD_REQUEST - -class DiffHandler(AnonymousBaseHandler): - allowed_methods = ('GET',) - - def read(self, request, uuid, revs, filename=None): - r = Repository(uuid) - if filename: - filename = hexdecode(filename) - - if not r.exists(): - return rc.NOT_FOUND - revs = revs.split(':') - for rev in revs: - if not r.hasrevision(rev): - return rc.BAD_REQUEST - - # Set maxsize to 100 kB or as requested, unless it's a single file, - # in which case serve the whole thing. The value of 80 kB was - # lovingly determined by trial and error. If you change it, - # please remember at least to do the former. - maxsize = request.GET.get('maxsize') if not filename else None - if maxsize: - maxsize = int(maxsize) - elif not filename: - maxsize = 80 * 1000 - - if len(revs) == 1: - udiff, bytecount, increment = r.diff(revs[0], filename=filename, maxsize=maxsize) - format_diffs(udiff) - else: - udiff, bytecount, increment = r.diff(revs[0], rev2=revs[1], filename=filename, maxsize=maxsize) - format_diffs(udiff) - - if filename: - return udiff[0] if udiff else rc.NOT_FOUND - else: - return {'type': 'diffs', - 'truncated': bytecount - maxsize > 0, - 'increment': increment, - 'diffs': udiff} - -class AutopullHandler(AnonymousBaseHandler): - allowed_methods = ('POST',) - - def create(self, request, uuid, uuid2): - r1 = Repository(uuid) - r2 = Repository(uuid2) - url = request.POST['website'] - if not r1.exists(): - return rc.NOT_FOUND - if not r2.exists(): - return rc.BAD_REQUEST - if not r1.isrelated(r2): - return error('repositories are not related', 'notrelated') - if not r2.outgoing(r1): - return error("repositories were already sync'd", 'alreadysyncd') - return {'type': 'push', 'success': r1.autopull(r2, url)} - -@with_pingbacks -class OutgoingHandler(AnonymousBaseHandler): - allowed_methods = ('GET', 'POST') - - def read(self, request, uuid, uuid2): - r1 = Repository(uuid) - r2 = Repository(uuid2) - nochangesets = int(request.GET.get('nochangesets', 0)) - if not r1.exists(): - return rc.NOT_FOUND - if not r2.exists(): - return rc.BAD_REQUEST - if not r1.isrelated(r2): - return error('repositories are not related', 'notrelated') - return {'type': 'outgoing', 'newheads': r1.pushwouldmakeheads(r2), 'changesets': [] if nochangesets else r1.outgoing(r2)} - - def create(self, request, uuid, uuid2): - r1 = Repository(uuid) - r2 = Repository(uuid2) - ixPerson = request.POST.get('ixPerson') - url = request.POST['website'] - if not r1.exists(): - return rc.NOT_FOUND - if not r2.exists(): - return rc.BAD_REQUEST - if not r1.isrelated(r2): - return error('repositories are not related', 'notrelated') - if not r1.outgoing(r2): - return error("repositories were already sync'd", 'alreadysyncd') - try: - return {'type': 'push', 'success': r1.push(r2, url, pusher=ixPerson)} - except RepositoryNotSubsetException, e: - return error(str(e), 'notstrictsubset') - except CreatesNewHeadsException, e: - return error(str(e), 'newheads') - -class SynchronizeHandler(AnonymousBaseHandler): - allowed_methods = ('POST',) - - def create(self, request): - if not settings.HOSTED: - return rc.BAD_REQUEST - remote = request.POST["remote"] - if 'repo' in request.POST: - resp = urllib2.urlopen(urlutil.urljoin(remote, "repo/%s/" % request.POST['repo'])) - repos = [simplejson.loads(resp.read())] - else: - resp = urllib2.urlopen(urlutil.urljoin(remote, "repo/")) - repos = simplejson.loads(resp.read()) - u = ui.ui() - u.setconfig('ui', 'quiet', 'True') - failures = [] - for repo in repos: - r = Repository(repo['uuid'], u) - remoteurl = urlutil.urljoin(remote, "repo/%s/serve" % r.uuid) - if not r.exists(): - r.create(repo['meta']) - try: - r.pull(remoteurl) - except Exception, e: - failures.append({'repo': repo['uuid'], 'exception': e}) - reportexception(e) - d = {'type': 'sync', 'success': not failures} - if failures: - d['failures'] = failures - return d - -class VersionHandler(AnonymousBaseHandler): - allowed_methods = ('GET',) - - def read(self, request): - return {'version': settings.KILN_BACKEND_VERSION} +@get('/repo/<uuid>/heads') +def get_heads(uuid): + r = Repository(uuid) + if not r.exists(): + raise NotFound + def _revtuple(rev): + "Return a (rev num, rev id) tuple from a changeset context" + return (rev.rev(), rev.hex()) + return {'heads': [_revtuple(r.repo[head]) for head in r.repo.heads()]}
Change 1 of 1 Show Entire File kiln/​api/​queuestats.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@@ -0,0 +1,62 @@
+from flask import request, render_template +import settings +from handlers import get, post, jsonify +from redis import Redis +from redis.cli import RedisCli + +queue_data = dict( + lists = [ + 'kiln:queue', + 'kiln:queue:high', + 'kiln:queue:low', + 'kiln:cancelations', + 'kiln:queue:running', + 'opengrok:index:running', + 'opengrok:cancelations', + ], + zsets = [ + 'opengrok:index', + ], + keys = [ + 'updaterepo:*:repo', + 'updaterepo:*:__failcount', + 'httppost:*:url', + 'httppost:*:__failcount', + ], +) + +def _get_redis(): + return Redis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_DB) + +@get('/queuestats/', as_json=False) +def queuestats(): + r = _get_redis() + data = {} + for l in queue_data['lists']: + data[l] = r.llen(l) + for z in queue_data['zsets']: + data[z] = r.zcard(z) + for k in queue_data['keys']: + data[k] = len(r.keys(k)) + if request.headers.get('X-Requested-With', '').lower() == 'xmlhttprequest': + return jsonify(data) + return render_template('queuestats.html', data=data) + +@post('/queuestats/redis/cli/') +def cli(): + cmd = request.form['cmd'] + try: + r = RedisCli(settings.REDIS_HOST, settings.REDIS_PORT).onecmd(cmd) + except Exception, e: + r = '*** Unknown exception: %s' % e + if r is None: + r = '' + elif isinstance(r, list): + r = '\n'.join(r) + d = dict(response=r) + if isinstance(r, basestring) and (r.startswith('Error') or r.startswith('***')): + d['type'] = 'error' + else: + d['type'] = 'success' + return d +
Show Entire File kiln/​api/​repositories.py Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Change 1 of 1 Show Entire File kiln/​api/​static/​jquery.color.js Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
@@ -0,0 +1,129 @@
+/* + * jQuery Color Animations + * Copyright 2007 John Resig + * Released under the MIT and GPL licenses. + */ + +(function(jQuery){ + + // We override the animation for all of these color styles + jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){ + jQuery.fx.step[attr] = function(fx){ + if ( !fx.colorInit ) { + fx.start = getColor( fx.elem, attr ); + fx.end = getRGB( fx.end ); + fx.colorInit = true; + } + + fx.elem.style[attr] = "rgb(" + [ + Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0), + Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0), + Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0) + ].join(",") + ")"; + } + }); + + // Color Conversion functions from highlightFade + // By Blair Mitchelmore + // http://jquery.offput.ca/highlightFade/ + + // Parse strings looking for color tuples [255,255,255] + function getRGB(color) { + var result; + + // Check if we're already dealing with an array of colors + if ( color && color.constructor == Array && color.length == 3 ) + return color; + + // Look for rgb(num,num,num) + if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)) + return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])]; + + // Look for rgb(num%,num%,num%) + if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color)) + return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55]; + + // Look for #a0b1c2 + if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color)) + return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)]; + + // Look for #fff + if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color)) + return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)]; + + // Look for rgba(0, 0, 0, 0) == transparent in Safari 3 + if (result = /rgba\(0, 0, 0, 0\)/.exec(color)) + return colors['transparent']; + + // Otherwise, we're most likely dealing with a named color + return colors[jQuery.trim(color).toLowerCase()]; + } + + function getColor(elem, attr) { + var color; + + do { + color = jQuery.curCSS(elem, attr); + + // Keep going until we find an element that has color, or we hit the body + if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") ) + break; + + attr = "backgroundColor"; + } while ( elem = elem.parentNode ); + + return getRGB(color); + }; + + // Some named colors to work with + // From Interface by Stefan Petre + // http://interface.eyecon.ro/ + + var colors = { + aqua:[0,255,255], + azure:[240,255,255], + beige:[245,245,220], + black:[0,0,0], + blue:[0,0,255], + brown:[165,42,42], + cyan:[0,255,255], + darkblue:[0,0,139], + darkcyan:[0,139,139], + darkgrey:[169,169,169], + darkgreen:[0,100,0], + darkkhaki:[189,183,107], + darkmagenta:[139,0,139], + darkolivegreen:[85,107,47], + darkorange:[255,140,0], + darkorchid:[153,50,204], + darkred:[139,0,0], + darksalmon:[233,150,122], + darkviolet:[148,0,211], + fuchsia:[255,0,255], + gold:[255,215,0], + green:[0,128,0], + indigo:[75,0,130], + khaki:[240,230,140], + lightblue:[173,216,230], + lightcyan:[224,255,255], + lightgreen:[144,238,144], + lightgrey:[211,211,211], + lightpink:[255,182,193], + lightyellow:[255,255,224], + lime:[0,255,0], + magenta:[255,0,255], + maroon:[128,0,0], + navy:[0,0,128], + olive:[128,128,0], + orange:[255,165,0], + pink:[255,192,203], + purple:[128,0,128], + violet:[128,0,128], + red:[255,0,0], + silver:[192,192,192], + white:[255,255,255], + yellow:[255,255,0], + transparent: [255,255,255] + }; + +})(jQuery);
This file's diff was not loaded because this changeset is very large. Load changes
This file's diff was not loaded because this changeset is very large. Load changes
This file's diff was not loaded because this changeset is very large. Load changes
Change 1 of 1 Show Entire File kiln/​api/​templates/​base.html Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
@@ -0,0 +1,12 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" > + <head> + <meta http-equiv="X-UA-Compatible" content="IE=8" /> + <title>{% block title %}{% endblock title %}</title> + <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> + {% block extra_head %}{% endblock extra_head %} + </head> + <body> + {% block content %}{% endblock content %} + </body> +</html>
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File kiln/​backend.py Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File kiln/​bfiles.py Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File kiln/​bugzscout.py Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File kiln/​client.crt Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File kiln/​client.key Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File kiln/​encoders.py Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File kiln/​errorloggingmiddleware.py Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File kiln/​httpshandler.py Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File kiln/​imports.py Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File kiln/​kilnext/​__init__.py Stacked
(No changes)
Show Entire File kiln/​kilnext/​nobinary.py Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File kiln/​kilnext/​nosymlink.py Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File kiln/​kilnext/​subzero.py Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Show Entire File kiln/​kilnhook.py Stacked
This file's diff was not loaded because this changeset is very large. Load changes
Change 1 of 1 Show Entire File kiln/​miniredis.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
@@ -0,0 +1,1305 @@
+#!/usr/bin/env python +# Copyright (C) 2011 Fog Creek Software. All rights reserved. + +from __future__ import with_statement + +import errno +import logging +import optparse +import os +import pickle +import random +import re +import select +import signal +import socket +import sys +import time + +from collections import deque as _deque +from logging import handlers + +from bugzscout import BugzScout + +LOGGER_NAME = 'miniredis' + +class deque(_deque): + def count(self, value): + matches = 0 + for x in self: + if x == value: + matches += 1 + return matches + +class RedisConstant(object): + def __init__(self, type): + self.type = type + + def __repr__(self): + return '<RedisConstant(%s)>' % self.type + +class RedisMessage(object): + def __init__(self, message): + self.message = message + + def __str__(self): + return '+%s' % self.message + + def __repr__(self): + return '<RedisMessage(%s)>' % self.message + +class RedisError(RedisMessage): + def __init__(self, message): + self.message = message + + def __str__(self): + return '-ERR %s' % self.message + + def __repr__(self): + return '<RedisError(%s)>' % self.message + +CHANGE_COMMANDS = ['del', 'rename', 'renamenx', 'expire', 'persist', 'move', 'flushdb', 'flushall', 'set', 'getset', 'setnx', 'setex', 'mset', 'msetnx', 'incr', 'incrby', 'decr', 'decrby', 'append', 'substr', 'rpush', 'lpush', 'ltrim', 'lset', 'lrem', 'lpop', 'rpop', 'blpop', 'brpop', 'rpoplpush', 'sadd', 'srem', 'spop', 'smove', 'sinterstore', 'sunionstore', 'sdiffstore', 'zadd', 'zrem', 'zincrby', 'zscore', 'zremrangebyrank', 'zremrangebyscore', 'zunionstore', 'hset', 'hmset', 'hincrby', 'hdel',] + +EMPTY_SCALAR = RedisConstant('EmptyScalar') +EMPTY_LIST = RedisConstant('EmptyList') +BAD_VALUE = RedisError('Operation against a key holding the wrong kind of value') + +def who(client): + try: + return '%s:%s' % client.socket.getpeername() if client else 'SERVER' + except: + return '<CLOSED>' + +def key_x(x): + def _getter(t): + return t[x] + return _getter + +def ishash(data): + return data is None or isinstance(data, dict) + +def islist(data): + return data is None or isinstance(data, deque) + +def isscalar(data): + return data is None or isinstance(data, basestring) or isinstance(data, int) or isinstance(data, float) + +def isset(data): + return data is None or isinstance(data, set) + +def issortedset(data): + return data is None or isinstance(data, dict) + +def slice(data, start, end=None): + if isinstance(data, deque): + data = list(data) + return data[start:end + 1 if end != -1 else None] + +def score_in_range(low, high): + def _cmp(t): + return t[1] >= low and t[1] <= high + return _cmp + + +def xfloat(x, low=True): + if isinstance(x, basestring): + if x == '-inf': + return sys.float_info.min + if x == 'inf': + return sys.float_info.max + if x.startswith('('): + if low: + return float(x[1:]) + sys.float_info.epsilon + else: + return float(x[1:]) - sys.float_info.epsilon + return float(x) + +class RedisClient(object): + def __init__(self, socket): + self.socket = socket + self.wfile = socket.makefile('wb') + self.rfile = socket.makefile('rb') + self.db = None + self.table = None + +class DisconnectedException(Exception): + def __init__(self, client): + super(DisconnectedException, self).__init__() + self.client = client + + def __str__(self): + return repr(self) + + def __repr__(self): + return '<DisconnectedException(%s)>' % who(self.client) + + +class MiniRedis(object): + def __init__(self, host='127.0.0.1', port=6379, log_file=None, db_file=None): + super(MiniRedis, self).__init__() + self.host = host + self.port = port + self.logger = logging.getLogger(LOGGER_NAME) + if log_file: + handler = handlers.RotatingFileHandler(log_file, maxBytes=0, backupCount=0) + handler.formatter = logging.Formatter('[%(process)d] %(asctime)s - %(levelname)s %(message)s', '%d %b %H:%M:%S') + self.logger.addHandler(handler) + self.logger.setLevel(logging.DEBUG) + + self.scout = BugzScout(LOGGER_NAME) + self.halt = True + + self.clients = {} + self.tables = {} + self.db_file = db_file + self.lastsave = int(time.time()) + + self.connections_received = 0 + self.commands_processed = 0 + self.changes_since_last_save = 0 + self.start_time = time.time() + + self.load() + + def critical(self, client, s): + if isinstance(s, list): + s = ''.join(s) + self.logger.critical('%s: %s' % (who(client), s)) + + def debug(self, client, s): + if isinstance(s, list): + s = ''.join(s) + self.logger.debug('%s: %s' % (who(client), s)) + + def dump(self, client, o): + nl = '\r\n' + if isinstance(o, bool): + if o: + client.wfile.write('+OK\r\n') + # Show nothing for a false return; that means be quiet + elif o == EMPTY_SCALAR: + client.wfile.write('$-1\r\n') + elif o == EMPTY_LIST: + client.wfile.write('*-1\r\n') + elif isinstance(o, int): + client.wfile.write(':' + str(o) + nl) + elif isinstance(o, float): + client.wfile.write('$' + str(len(str(o))) + nl) + client.wfile.write(str(o) + nl) + elif isinstance(o, str): + client.wfile.write('$' + str(len(o)) + nl) + client.wfile.write(o + nl) + elif isinstance(o, list): + client.wfile.write('*' + str(len(o)) + nl) + for val in o: + self.dump(client, val) + elif isinstance(o, RedisMessage): + client.wfile.write('%s\r\n' % o) + else: + client.wfile.write('return type not yet implemented\r\n') + client.wfile.flush() + + def load(self): + if self.db_file and os.path.lexists(self.db_file): + with open(self.db_file, 'rb') as f: + try: + self.tables = pickle.load(f) + self.log(None, 'loaded database from file "%s"' % self.db_file) + except: + self.log(None, 'database was old format; load aborted') + + def log(self, client, s): + if isinstance(s, list): + s = ''.join(s) + self.logger.info('%s: %s', who(client), s) + + def handle(self, client): + line = client.rfile.readline() + if not line: + raise DisconnectedException(client) + items = int(line[1:].strip()) + args = [] + for x in xrange(0, items): + length = int(client.rfile.readline().strip()[1:]) + args.append(client.rfile.read(length)) + client.rfile.read(2) # throw out newline + command = args[0].lower() + self.dump(client, getattr(self, 'handle_' + command)(client, *args[1:])) + self.commands_processed += 1 + if command in CHANGE_COMMANDS: + self.changes_since_last_save += 1 + + def rotate(self): + for handler in self.logger.handlers: + if hasattr(handler, 'doRollover'): + handler.doRollover() + + def run(self): + self.halt = False + server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + server.bind((self.host, self.port)) + server.listen(5) + while not self.halt: + try: + readable, _, _ = select.select([server] + self.clients.keys(), [], [], 1.0) + except select.error, e: + if e.args[0] == errno.EINTR: + continue + raise + for sock in readable: + if sock == server: + (client_socket, address) = server.accept() + client = RedisClient(client_socket) + self.clients[client_socket] = client + self.log(client, 'client connected') + self.select(client, 0) + else: + client = self.clients[sock] + try: + self.handle(client) + except Exception, e: + if isinstance(e, DisconnectedException): + self.log(None, 'client disconnected: %s' % e) + else: + import traceback + self.critical(client, 'EXCEPTION: %s' % e) + self.critical(client, traceback.format_exc()) + self.handle_quit(client) + for client_socket in self.clients.iterkeys(): + client_socket.close() + self.clients.clear() + server.close() + + def save(self): + if self.db_file: + with open(self.db_file, 'wb') as f: + pickle.dump(self.tables, f, pickle.HIGHEST_PROTOCOL) + self.lastsave = int(time.time()) + self.changes_since_last_save = 0 + + def select(self, client, db): + if db not in self.tables: + self.tables[db] = {} + client.db = db + client.table = self.tables[db] + self.connections_received += 1 + + def stop(self): + if not self.halt: + self.log(None, 'STOPPING') + self.save() + self.halt = True + + def who(self, client): + try: + who = '%s:%s' % client.socket.getpeername() if client else 'SERVER' + except: + who = '<CLOSED>' + return who + + # HANDLERS + + def handle_append(self, client, key, value): + data = client.table.get(key, '') + if not isscalar(data): + return BAD_VALUE + client.table[key] = str(data) + str(value) + self.debug(client, 'APPEND %s %s -> %s' % (key, value, client.table[key])) + return len(client.table[key]) + + def handle_bgsave(self, client): + if hasattr(os, 'fork'): + if not os.fork(): + self.save() + sys.exit(0) + else: + self.save() + self.debug(client, 'BGSAVE') + return RedisMessage('Background saving started') + + def handle_dbsize(self, client): + return len(client.table) + + def handle_decr(self, client, key): + return self.handle_decrby(client, key, 1) + + def handle_decrby(self, client, key, by): + try: + by = -1 * int(by) + except (TypeError, ValueError): + return BAD_VALUE + return self.handle_incrby(client, key, by) + + def handle_del(self, client, key): + self.debug(client, 'DEL %s' % key) + if key not in client.table: + return 0 + del client.table[key] + return 1 + + def handle_exists(self, client, key): + self.debug(client, 'EXISTS %s' % key) + if key in client.table: + return 1 + return 0 + + def handle_flushdb(self, client): + self.debug(client, 'FLUSHDB') + client.table.clear() + return True + + def handle_flushall(self, client): + self.debug(client, 'FLUSHALL') + for table in self.tables.itervalues(): + table.clear() + return True + + def handle_get(self, client, key): + data = client.table.get(key, None) + if not isscalar(data): + return BAD_VALUE + if data != None: + data = str(data) + else: + data = EMPTY_SCALAR + self.debug(client, 'GET %s -> %s' % (key, data)) + return data + + def handle_getset(self, client, key, new): + data = client.table.get(key, None) + if not isscalar(data): + return BAD_VALUE + if data != None: + data = str(data) + else: + data = EMPTY_SCALAR + self.debug(client, 'GETSET %s %s -> %s' % (key, new, data)) + client.table[key] = new + return data + + # Hash Functions + def handle_hdel(self, client, key, field): + hashtable = client.table.get(key, {}) + if not ishash(hashtable): + return BAD_VALUE + exists = hashtable.has_key(field) + if exists: + hashtable.pop(field) + self.debug(client, 'HDEL %s %s -> %s' % (key, field, exists)) + return 1 if exists else 0 + + def handle_hexists(self, client, key, field): + hashtable = client.table.get(key, {}) + if not ishash(hashtable): + return BAD_VALUE + exists = hashtable.has_key(field) + self.debug(client, 'HEXISTS %s %s -> %s' % (key, field, exists)) + return 1 if exists else 0 + + def handle_hget(self, client, key, field): + hashtable = client.table.get(key, {}) + if not ishash(hashtable): + return BAD_VALUE + if not hashtable.has_key(field): + return EMPTY_LIST + value = hashtable[field] + self.debug(client, 'HGET %s %s -> %s' % (key, field, value)) + return value + + def handle_hgetall(self, client, key): + hashtable = client.table.get(key, {}) + if not ishash(hashtable): + return BAD_VALUE + all = [] + for key, value in hashtable.iteritems(): + all.append(key) + all.append(value) + self.debug(client, 'HGETALL %s' % (key,)) + return all + + def handle_hincrby(self, client, key, field, by): + hashtable = client.table.get(key, None) + if hashtable is None: + hashtable = {} + client.table[key] = hashtable + if not ishash(hashtable): + return BAD_VALUE + data = hashtable.get(field, 0) + try: + data = int(data) + except (TypeError, ValueError): + return BAD_VALUE + try: + by = int(by) + except (TypeError, ValueError): + return RedisError('value is not an integer') + hashtable[field] = data + by + self.debug(client, 'HINCRBY %s %s %s -> %s' % (key, field, by, client.table[key][field])) + return client.table[key][field] + + def handle_hkeys(self, client, key): + hashtable = client.table.get(key, {}) + if not ishash(hashtable): + return BAD_VALUE + keys = hashtable.keys() + self.debug(client, 'HKEYS %s' % (key,)) + return keys + + def handle_hlen(self, client, key): + hashtable = client.table.get(key, {}) + if not ishash(hashtable): + return BAD_VALUE + value = len(hashtable) + self.debug(client, 'HLEN %s -> %s' % (key, value)) + return value + + def handle_hmget(self, client, key, *args): + hashtable = client.table.get(key, {}) + if not ishash(hashtable): + return BAD_VALUE + if not args: + return RedisError("wrong number of arguments for 'hmget' command") + values = [] + for field in args: + values.append(hashtable.get(field, EMPTY_SCALAR)) + self.debug(client, 'HMGET %s %s' % (key, ' '.join(args))) + return values + + def handle_hmset(self, client, key, *args): + hashtable = client.table.get(key, None) + if hashtable is None: + hashtable = {} + client.table[key] = hashtable + if not ishash(hashtable): + return BAD_VALUE + if not args or len(args) % 2 > 0: + return RedisError('wrong number of arguments for HMSET') + for field, value in zip(args[::2], args[1::2]): + hashtable[field] = value + self.debug(client, 'HMSET %s %s' % (key, ' '.join(args))) + return True + + def handle_hset(self, client, key, field, value): + hashtable = client.table.get(key, None) + if hashtable is None: + hashtable = {} + client.table[key] = hashtable + if not ishash(hashtable): + return BAD_VALUE + field_exists = hashtable.has_key(field) + hashtable[field] = value + self.debug(client, 'HSET %s %s %s' % (key, field, value)) + return 0 if field_exists else 1 + + def handle_hsetnx(self, client, key, field, value): + hashtable = client.table.get(key, None) + if hashtable is None: + hashtable = {} + client.table[key] = hashtable + if not ishash(hashtable): + return BAD_VALUE + field_exists = hashtable.has_key(field) + if not field_exists: + hashtable[field] = value + self.debug(client, 'HSETNX %s %s %s' % (key, field, value)) + return 0 if field_exists else 1 + + def handle_hvals(self, client, key): + hashtable = client.table.get(key, {}) + if not ishash(hashtable): + return BAD_VALUE + values = hashtable.values() + self.debug(client, 'HKEYS %s' % (key,)) + return values + + def handle_incr(self, client, key): + return self.handle_incrby(client, key, 1) + + def handle_incrby(self, client, key, by): + data = client.table.get(key, 0) + try: + data = int(data) + except (TypeError, ValueError): + return BAD_VALUE + try: + by = int(by) + except (TypeError, ValueError): + return RedisError('value is not an integer') + client.table[key] = data + by + self.debug(client, 'INCRBY %s %s -> %s' % (key, by, client.table[key])) + return client.table[key] + + def handle_info(self, client): + self.debug(client, 'INFO') + uptime = time.time()-self.start_time + return """redis_version:1.3.17 +connected_clients:%d +connected_slaves:0 +used_memory:%d +changes_since_last_save:%d +last_save_time:%d +total_connections_received:%d +total_commands_processed:%d +uptime_in_seconds:%.2f +uptime_in_days:%.2f +db%d:keys=%d,expires=0""" % (len(self.clients), client.table.__sizeof__(), self.lastsave, self.changes_since_last_save, self.connections_received, self.commands_processed, uptime, uptime / 86400.0, client.db, len(client.table)) + + def handle_keys(self, client, pattern): + r = re.compile(pattern.replace('*', '.*')) + self.debug(client, 'KEYS %s' % pattern) + return [k for k in client.table.keys() if r.search(k)] + + def handle_lastsave(self, client): + return self.lastsave + + def handle_lindex(self, client, key, index): + index = int(index) + if key not in client.table: + return EMPTY_LIST + if not islist(client.table[key]): + return BAD_VALUE + try: + data = list(client.table[key])[index] + except IndexError: + return EMPTY_SCALAR + self.debug(client, 'LINDEX %s %s -> %s' % (key, index, data)) + return data + + def handle_llen(self, client, key): + if key not in client.table: + return 0 + if not islist(client.table[key]): + return BAD_VALUE + return len(client.table[key]) + + def handle_lpop(self, client, key): + if key not in client.table: + return EMPTY_SCALAR + if not islist(client.table[key]): + return BAD_VALUE + if len(client.table[key]) > 0: + data = client.table[key].popleft() + else: + data = EMPTY_SCALAR + self.debug(client, 'LPOP %s -> %s' % (key, data)) + return data + + def handle_lpush(self, client, key, data): + if key not in client.table: + client.table[key] = deque() + elif not islist(client.table[key]): + return BAD_VALUE + client.table[key].appendleft(data) + self.debug(client, 'LPUSH %s %s' % (key, data)) + return len(client.table[key]) + + def handle_lrange(self, client, key, low, high): + low, high = int(low), int(high) + if key not in client.table: + return [] + if not islist(client.table[key]): + return BAD_VALUE + l = slice(client.table[key], low, high) + self.debug(client, 'LRANGE %s %s %s -> %s' % (key, low, high-1 if high else -1, l)) + return l + + def handle_lrem(self, client, key, count, value): + if key not in client.table: + return 0 + if not islist(client.table[key]): + return BAD_VALUE + old_list = client.table[key] + count = int(count) + reverse = (count < 0) + if count == 0 or abs(count) >= old_list.count(value): + # Removing more than exist. Front or back doesn't matter. + new_list = deque((x for x in old_list if x != value)) + removed = len(old_list) - len(new_list) + client.table[key] = new_list + else: + # More elements matching value than count. + new_list = old_list + if reverse: + count *= -1 + new_list = deque(reversed(new_list)) + removed = 0 + to_remove = count + while to_remove > 0: + # No, it's not the most efficient way if count is large... + new_list.remove(value) + to_remove -= 1 + removed += 1 + if reverse: + new_list = deque(reversed(new_list)) + client.table[key] = new_list + self.debug(client, 'LREM %s %s %s -> %s' % (key, count, value, removed)) + return removed + + def handle_lset(self, client, key, index, value): + if key not in client.table: + client.table[key] = deque() + elif not islist(client.table[key]): + return BAD_VALUE + try: + index = int(index) + except (TypeError, ValueError): + return RedisError('value is not an integer') + try: + client.table[key][index] = value + except IndexError: + return RedisError('index out of range') + self.debug(client, 'LSET %s %s %s' % (key, index, value)) + return True + + def handle_ltrim(self, client, key, start, end): + data = client.table.get(key, None) + if not islist(data): + return BAD_VALUE + try: + start = int(start) + end = int(end) + except (TypeError, ValueError): + return RedisError('value is not an integer') + if data: + client.table[key] = deque(slice(data, start, end)) + return True + + def handle_mget(self, client, *args): + if not args: + return RedisError("wrong number of arguments for 'hmget' command") + values = [] + for key in args: + data = client.table.get(key, None) + if not isscalar(data): + data = None + values.append(data or EMPTY_SCALAR) + self.debug(client, 'MGET %s %s' % (key, ' '.join(args))) + return values + + def handle_mset(self, client, *args): + if not args or len(args) % 2 > 0: + return RedisError('wrong number of arguments for MSET') + for key, value in zip(args[::2], args[1::2]): + client.table[key] = value + self.debug(client, 'MSET %s' % (' '.join(args))) + return True + + def handle_msetnx(self, client, *args): + if not args or len(args) % 2 > 0: + return RedisError('wrong number of arguments for MSET') + for key in args[::2]: + if key in client.table: + return 0 + for key, value in zip(args[::2], args[1::2]): + client.table[key] = value + self.debug(client, 'MSET %s' % (' '.join(args))) + return 1 + + def handle_ping(self, client): + self.debug(client, 'PING -> PONG') + return RedisMessage('PONG') + + def handle_randomkey(self, client): + self.debug(client, 'RANDOMKEY') + if not len(client.table): + return EMPTY_SCALAR + return random.choice(client.table.keys()) + + def handle_rename(self, client, old, new): + if old == new: + return RedisError('source and destination objects are the same') + if old not in client.table: + return RedisError('no such key') + self.debug(client, 'RENAME %s %s' % (old, new)) + client.table[new] = client.table.pop(old) + return True + + def handle_renamenx(self, client, old, new): + if old == new: + return RedisError('source and destination objects are the same') + if old not in client.table: + return RedisError('no such key') + self.debug(client, 'RENAMENX %s %s' % (old, new)) + if new in client.table: + return 0 + client.table[new] = client.table.pop(old) + return 1 + + def handle_rpop(self, client, key): + if key not in client.table: + return EMPTY_SCALAR + if not islist(client.table[key]): + return BAD_VALUE + if len(client.table[key]) > 0: + data = client.table[key].pop() + else: + data = EMPTY_SCALAR + self.debug(client, 'RPOP %s -> %s' % (key, data)) + return data + + def handle_rpoplpush(self, client, src, dest): + if src not in client.table: + return EMPTY_SCALAR + if dest not in client.table: + client.table[dest] = deque() + if not islist(client.table[src]) or not islist(client.table[dest]): + return BAD_VALUE + if len(client.table[src]) == 0: + return EMPTY_SCALAR + data = client.table[src].pop() + client.table[dest].appendleft(data) + self.debug(client, 'RPOPLPUSH %s %s -> %s' % (src, dest, data)) + return data + + def handle_rpush(self, client, key, data): + if key not in client.table: + client.table[key] = deque() + elif not islist(client.table[key]): + return BAD_VALUE + client.table[key].append(data) + self.debug(client, 'RPUSH %s %s' % (key, data)) + return len(client.table[key]) + + def handle_type(self, client, key): + if key not in client.table: + return RedisMessage('none') + + data = client.table[key] + if islist(data): + return RedisMessage('list') + elif isset(data): + return RedisMessage('set') + elif ishash(data): + return RedisMessage('hash') + elif isscalar(data): + return RedisMessage('string') + else: + return RedisError('unknown data type') + + def handle_quit(self, client): + self.debug(client, 'QUIT') + if client.socket in self.clients: + del self.clients[client.socket] + try: + client.socket.shutdown(socket.SHUT_RDWR) + except socket.error: + pass + finally: + try: + client.socket.close() + except socket.error: + pass + return False + + def handle_sadd(self, client, key, data): + if key not in client.table: + client.table[key] = set() + elif not isset(client.table[key]): + return BAD_VALUE + exists = 0 + if data not in client.table[key]: + client.table[key].add(data) + exists = 1 + self.debug(client, 'SADD %s %s' % (key, data)) + return exists + + def handle_scard(self, client, key): + if key not in client.table: + return 0 + elif not isset(client.table[key]): + return BAD_VALUE + else: + data = len(client.table[key]) + self.debug(client, 'SCARD %s -> %d' % (key, data)) + return data + + def handle_sdiff(self, client, key, *keys): + if key not in client.table: + return [] + elif not isset(client.table[key]): + return BAD_VALUE + result = client.table[key] + for diff_key in keys: + if diff_key not in client.table: + continue + elif not isset(client.table[diff_key]): + return BAD_VALUE + result = result.difference(client.table[diff_key]) + self.debug(client, 'SDIFF %s %s' % (key, ' '.join(keys))) + return list(result) + + def handle_sdiffstore(self, client, dest, key, *keys): + result = self.handle_sdiff(client, key, *keys) + if not isinstance(result, list): + return BAD_VALUE + client.table[dest] = set(result) + self.debug(client, 'SDIFFSTORE %s %s %s' % (dest, key, ' '.join(keys))) + return len(result) + + def handle_sinter(self, client, key, *keys): + if key not in client.table: + return [] + elif not isset(client.table[key]): + return BAD_VALUE + result = client.table[key] + for inter_key in keys: + if inter_key not in client.table: + continue + elif not isset(client.table[inter_key]): + return BAD_VALUE + result = result.intersection(client.table[inter_key]) + self.debug(client, 'SINTER %s %s' % (key, ' '.join(keys))) + return list(result) + + def handle_sinterstore(self, client, dest, key, *keys): + result = self.handle_sinter(client, key, *keys) + if not isinstance(result, list): + return BAD_VALUE + client.table[dest] = set(result) + self.debug(client, 'SINTERSTORE %s %s %s' % (dest, key, ' '.join(keys))) + return len(result) + + def handle_sismember(self, client, key, data): + if key not in client.table: + return 0 + elif not isset(client.table[key]): + return BAD_VALUE + self.debug(client, 'SISMEMBER %s %s' % (key, data)) + return 1 if data in client.table[key] else 0 + + def handle_smembers(self, client, key): + if key not in client.table: + client.table[key] = set() + elif not isset(client.table[key]): + return BAD_VALUE + self.debug(client, 'SMEMBERS %s' % (key,)) + return list(client.table[key]) + + def handle_smove(self, client, src, dest, data): + if src not in client.table: + return EMPTY_LIST + elif not isset(client.table[src]): + return BAD_VALUE + if dest not in client.table: + client.table[dest] = set() + elif not isset(client.table[dest]): + return BAD_VALUE + exists = 0 + if data in client.table[src]: + client.table[src].remove(data) + client.table[dest].add(data) + exists = 1 + self.debug(client, 'SMOVE %s %s %s' % (src, dest, data,)) + return exists + + def handle_spop(self, client, key): + if key not in client.table: + return EMPTY_LIST + elif not isset(client.table[key]): + return BAD_VALUE + try: + data = random.sample(client.table[key], 1)[0] + client.table[key].remove(data) + except (KeyError, ValueError): + data = EMPTY_LIST + self.debug(client, 'SPOP %s -> %s' % (key, data)) + return data + + def handle_srandmember(self, client, key): + if key not in client.table: + return EMPTY_LIST + elif not isset(client.table[key]): + return BAD_VALUE + try: + data = random.sample(client.table[key], 1)[0] + except (KeyError, ValueError): + data = EMPTY_LIST + self.debug(client, 'SRANDMEMBER %s -> %s' % (key, data)) + return data + + def handle_srem(self, client, key, data): + if key not in client.table: + client.table[key] = set() + elif not isset(client.table[key]): + return BAD_VALUE + exists = 0 + if data in client.table[key]: + client.table[key].remove(data) + exists = 1 + self.debug(client, 'SREM %s %s' % (key, data)) + return exists + + def handle_substr(self, client, key, start, end): + data = client.table.get(key, '') + if not isscalar(data): + return BAD_VALUE + try: + start = int(start) + end = int(end) + except (TypeError, ValueError): + return RedisError('value is not an integer') + data = slice(str(data), start, end) + self.debug(client, 'SUBSTR %s %s %s -> %s' % (key, start, end, data)) + return data + + def handle_sunion(self, client, key, *keys): + if key not in client.table: + return [] + elif not isset(client.table[key]): + return BAD_VALUE + result = client.table[key] + for union_key in keys: + if union_key not in client.table: + continue + elif not isset(client.table[union_key]): + return BAD_VALUE + result = result.union(client.table[union_key]) + self.debug(client, 'SUNION %s %s' % (key, ' '.join(keys))) + return list(result) + + def handle_sunionstore(self, client, dest, key, *keys): + result = self.handle_sunion(client, key, *keys) + if not isinstance(result, list): + return BAD_VALUE + client.table[dest] = set(result) + self.debug(client, 'SUNIONSTORE %s %s %s' % (dest, key, ' '.join(keys))) + return len(result) + + def handle_save(self, client): + self.save() + self.debug(client, 'SAVE') + return True + + def handle_select(self, client, db): + db = int(db) + self.select(client, db) + self.debug(client, 'SELECT %s' % db) + return True + + def handle_set(self, client, key, data): + client.table[key] = data + self.debug(client, 'SET %s -> %s' % (key, data)) + return True + + def handle_setnx(self, client, key, data): + if key in client.table: + self.debug(client, 'SETNX %s -> %s FAILED' % (key, data)) + return 0 + client.table[key] = data + self.debug(client, 'SETNX %s -> %s' % (key, data)) + return 1 + + def handle_shutdown(self, client): + self.debug(client, 'SHUTDOWN') + self.halt = True + self.save() + return self.handle_quit(client) + + def handle_zadd(self, client, key, score, member): + if key not in client.table: + client.table[key] = dict() + elif not issortedset(client.table[key]): + return BAD_VALUE + try: + score = float(score) + except (TypeError, ValueError): + return BAD_VALUE + exists = 0 + if member not in client.table[key]: + client.table[key][member] = score + exists = 1 + self.debug(client, 'ZADD %s %s %s' % (key, member, score)) + return exists + + def handle_zrem(self, client, key, member): + if key not in client.table: + client.table[key] = dict() + elif not issortedset(client.table[key]): + return BAD_VALUE + exists = 0 + if member in client.table[key]: + client.table[key].pop(member) + exists = 1 + self.debug(client, 'ZREM %s %s' % (key, member)) + return exists + + def handle_zincrby(self, client, key, score, member): + if key not in client.table: + client.table[key] = dict() + elif not issortedset(client.table[key]): + return BAD_VALUE + try: + score = float(score) + except (TypeError, ValueError): + return RedisError('value is not an integer') + client.table[key].setdefault(member, 0.0) + client.table[key][member] += score + self.debug(client, 'ZINCRBY %s %s -> %s' % (key, score, client.table[key])) + return client.table[key][member] + + def handle_zrank(self, client, key, member, reverse=False): + if key not in client.table: + client.table[key] = dict() + elif not issortedset(client.table[key]): + return BAD_VALUE + if member not in client.table[key]: + return EMPTY_SCALAR + l = sorted(client.table[key].values(), reverse=reverse).index(client.table[key][member]) + self.debug(client, 'Z%sRANK %s %s -> %s' % ('REV' if reverse else '', key, member, l)) + return l + + def handle_zrevrank(self, client, key, member): + return self.handle_zrank(client, key, member, True) + + def handle_zrange(self, client, key, low, high, withscores=False, reverse=False): + try: + low, high = int(low), int(high) + except (TypeError, ValueError): + return BAD_VALUE + if key not in client.table: + return [] + if not issortedset(client.table[key]): + return BAD_VALUE + l = slice(sorted(client.table[key].items(), key=key_x(1), reverse=reverse), low, high) + self.debug(client, 'Z%sRANGE %s %s %s -> %s' % ('REV' if reverse else '', key, low, high-1 if high else -1, l)) + if withscores: + return list(sum(l, ())) + return [t[0] for t in l] + + def handle_zrevrange(self, client, key, low, high, withscores=False): + return self.handle_zrange(client, key, low, high, withscores=withscores, reverse=True) + + def handle_zcount(self, client, key, low, high): + try: + low, high = xfloat(low), xfloat(high) + except (TypeError, ValueError): + return BAD_VALUE + if key not in client.table: + return [] + if not issortedset(client.table[key]): + return BAD_VALUE + l = filter(score_in_range(low, high), sorted(client.table[key].items(), key=key_x(1))) + self.debug(client, 'ZCOUNT %s %s %s -> %s' % (key, low, high, len(l))) + return len(l) + + def handle_zrangebyscore(self, client, key, low, high, limit=False, offset=None, count=None, withscores=False): + try: + low, high = xfloat(low), xfloat(high) + except (TypeError, ValueError): + return BAD_VALUE + if key not in client.table: + return [] + if not issortedset(client.table[key]): + return BAD_VALUE + l = sorted(filter(score_in_range(low, high), client.table[key].items()), key=key_x(1)) + if limit: + if offset is not None and count is not None: + try: + offset, count = int(offset), int(count) + except (TypeError, ValueError): + return BAD_VALUE + l = slice(l, offset, count) + else: + withscores = True + self.debug(client, 'ZRANGEBYSCORE %s %s %s -> %s' % (key, low, high, l)) + if withscores: + return list(sum(l, ())) + return [t[0] for t in l] + + def handle_zremrangebyscore(self, client, key, low, high): + try: + low, high = xfloat(low), xfloat(high) + except (TypeError, ValueError): + return BAD_VALUE + if key not in client.table: + return [] + if not issortedset(client.table[key]): + return BAD_VALUE + l = sorted(filter(score_in_range(low, high), client.table[key].items()), key=key_x(1)) + l = [t[0] for t in l] + self.debug(client, 'ZREMRANGEBYSCORE %s %s %s -> %s' % (key, low, high, len(l))) + for x in l: + client.table[key].pop(x) + return len(l) + + def handle_zremrangebyrank(self, client, key, low, high, withscores=False, reverse=False): + try: + low, high = int(low), int(high) + except (TypeError, ValueError): + return BAD_VALUE + if key not in client.table: + return [] + if not issortedset(client.table[key]): + return BAD_VALUE + l = slice(sorted(client.table[key].items(), key=key_x(1), reverse=reverse), low, high) + l = [t[0] for t in l] + self.debug(client, 'Z%sRANGE %s %s %s -> %s' % ('REV' if reverse else '', key, low, high-1 if high else -1, l)) + for x in l: + client.table[key].pop(x) + return len(l) + + def handle_zcard(self, client, key): + if key not in client.table: + client.table[key] = dict() + elif not issortedset(client.table[key]): + return BAD_VALUE + data = len(client.table[key]) + self.debug(client, 'ZCARD %s -> %s' % (key, data)) + return data + + def handle_zscore(self, client, key, member): + if key not in client.table: + client.table[key] = dict() + elif not issortedset(client.table[key]): + return BAD_VALUE + if member not in client.table[key]: + return EMPTY_SCALAR + data = client.table[key][member] + self.debug(client, 'ZSCORE %s %s -> %s' % (key, member, data)) + return data + +def create_daemon(): + """Detach a process from the controlling terminal and run it in the + background as a daemon. + """ + + if os.name != 'posix': + # sorry, you can't daemonize on Windows + return + + import resource # Import here, because Windows doesn't have it. + + try: + pid = os.fork() + except OSError, e: + raise Exception, "%s [%d]" % (e.strerror, e.errno) + + if pid == 0: + os.setsid() + + try: + # Fork a second child and exit immediately to prevent zombies. This + # causes the second child process to be orphaned, making the init + # process responsible for its cleanup. And, since the first child is + # a session leader without a controlling terminal, it's possible for + # it to acquire one by opening a terminal in the future (System V- + # based systems). This second fork guarantees that the child is no + # longer a session leader, preventing the daemon from ever acquiring + # a controlling terminal. + pid = os.fork() + except OSError, e: + raise Exception, "%s [%d]" % (e.strerror, e.errno) + + if pid == 0: + # Pick sane working dir and umask + os.chdir('/') + os.umask(0022) + else: + # use os._exit() to avoid double-flushing stdin/stdout + os._exit(0) # Exit parent (the first child) of the second child. + else: + os._exit(0) # Exit parent of the first child. + + # Close all open file descriptors. This prevents the child from keeping + # open any file descriptors inherited from the parent. There is a variety + # of methods to accomplish this task. Three are listed below. + # + # Use the getrlimit method to retrieve the maximum file descriptor number + # that can be opened by this process. If there is not limit on the + # resource, use 1024. + # + maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] + if maxfd == resource.RLIM_INFINITY: + maxfd = 1024 + + for fd in xrange(maxfd): + try: + os.close(fd) + except OSError: + pass + + # redirect stdout/stderr to /dev/null + os.open(os.devnull, os.O_RDWR) + + # Duplicate standard input to standard output and standard error. + os.dup2(0, 1) + os.dup2(0, 2) + + return 0 + +def main(args): + global logger + logger = logging.getLogger(LOGGER_NAME) + if not logger.handlers: + logger.addHandler(logging.StreamHandler()) + parser = optparse.OptionParser() + parser.add_option('--host', help='address to listen on', + default='127.0.0.1') + parser.add_option('-p', '--port', type='int', help='port to listen on', + default=6379) + parser.add_option('-d', '--dbfile', help='database file name', + dest='db_file') + parser.add_option('-l', '--logfile', help='file to log to', + dest='log_file') + parser.add_option('--daemon', action='store_true', dest='daemonize', + help='run as a daemon (Unix only)') + parser.add_option('--user', help='user for daemon to run as') + parser.add_option('--group', help='group for daemon to run as') + parser.add_option('--pid', help='name of pid file', dest='pid_file') + + opts, args = parser.parse_args() + + host = opts.host + port = opts.port + log_file = os.path.abspath(opts.log_file) if opts.log_file else None + pid_file = os.path.abspath(opts.pid_file) if opts.pid_file else None + db_file = os.path.abspath(opts.db_file) if opts.db_file else None + user = opts.user + group = opts.group + daemonize = opts.daemonize + + unix = os.name == 'posix' + + if user or group: + if not unix: + logger.error('impersonation only works on Unix') + else: + try: + if group: + import grp + os.setgid(grp.getgrnam(group)[2]) + if user: + import pwd + os.setuid(pwd.getpwnam(user)[2]) + except: + logger.error('insufficient permissions to run as %s:%s', user, group) + + if daemonize: + create_daemon() + + if pid_file: + with open(pid_file, 'w') as f: + f.write('%s\n' % os.getpid()) + + if unix: + def sigterm(signum, frame): + m.stop() + def sighup(signum, frame): + m.rotate() + signal.signal(signal.SIGTERM, sigterm) + signal.signal(signal.SIGHUP, sighup) + + m = MiniRedis(host=host, port=port, log_file=log_file, db_file=db_file) + try: + m.run() + except KeyboardInterrupt: + m.stop() + except Exception, e: + m.scout.report_exception(e) + if pid_file: + os.unlink(pid_file) + sys.exit(0) + +if __name__ == '__main__': + main(sys.argv[1:])
Change 1 of 1 Show Entire File kiln/​ourdot.sh Stacked
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
@@ -0,0 +1,7 @@
+#!/bin/sh + +if [ ! -e ~/miniredis.pid ]; then + nohup python miniredis.py -p 56784 -d ~/miniredis.db -l ~/miniredis.out --pid ~/miniredis.pid & +else + echo MiniRedis is running, or crashed +fi