Просмотр исходного кода

Merge pull request #44 from sharelatex/add-chktex-support

Add chktex support
Brian Gough 10 лет назад
Родитель
Сommit
5c1e903b14

+ 11 - 2
services/clsi/app/coffee/CommandRunner.coffee

@@ -4,13 +4,18 @@ logger = require "logger-sharelatex"
 logger.info "using standard command runner"
 
 module.exports = CommandRunner =
-	run: (project_id, command, directory, image, timeout, callback = (error) ->) ->
+	run: (project_id, command, directory, image, timeout, environment, callback = (error) ->) ->
 		command = (arg.replace('$COMPILE_DIR', directory) for arg in command)
 		logger.log project_id: project_id, command: command, directory: directory, "running command"
 		logger.warn "timeouts and sandboxing are not enabled with CommandRunner"
 
+		# merge environment settings
+		env = {}
+		env[key] = value for key, value of process.env
+		env[key] = value for key, value of environment
+
 		# run command as detached process so it has its own process group (which can be killed if needed)
-		proc = spawn command[0], command.slice(1), stdio: "inherit", cwd: directory, detached: true
+		proc = spawn command[0], command.slice(1), stdio: "inherit", cwd: directory, detached: true, env: env
 
 		proc.on "error", (err)->
 			logger.err err:err, project_id:project_id, command: command, directory: directory, "error running command"
@@ -22,6 +27,10 @@ module.exports = CommandRunner =
 				err = new Error("terminated")
 				err.terminated = true
 				return callback(err)
+			else if code
+				err = new Error("exit")
+				err.code = code
+				return callback(err)
 			else
 				callback()
 

+ 2 - 0
services/clsi/app/coffee/CompileController.coffee

@@ -17,6 +17,8 @@ module.exports = CompileController =
 				CompileManager.doCompile request, (error, outputFiles = []) ->
 					if error?.terminated
 						status = "terminated"
+					else if error?.validate
+						status = "validation-#{error.validate}"
 					else if error?
 						logger.error err: error, project_id: request.project_id, "error running compile"
 						if error.timedout

+ 16 - 2
services/clsi/app/coffee/CompileManager.coffee

@@ -41,7 +41,16 @@ module.exports = CompileManager =
 					DraftModeManager.injectDraftMode Path.join(compileDir, request.rootResourcePath), callback
 				else
 					callback()
-			
+
+			# set up environment variables for chktex
+			env = {}
+			if request.check?
+				env['CHKTEX_OPTIONS'] =  '-nall -e9 -e10 -w15 -w16 -w27'
+				if request.check is 'error'
+					env['CHKTEX_EXIT_ON_ERROR'] =  1
+				if request.check is 'validate'
+					env['CHKTEX_VALIDATE'] =  1
+
 			injectDraftModeIfRequired (error) ->
 				return callback(error) if error?
 				timer = new Metrics.Timer("run-compile")
@@ -57,9 +66,14 @@ module.exports = CompileManager =
 					compiler:  request.compiler
 					timeout:   request.timeout
 					image:     request.imageName
+					environment: env
 				}, (error, output, stats, timings) ->
+					if request.check is "validate"
+						result = if error?.code then "fail" else "pass"
+						error = new Error("validation")
+						error.validate = result
 					# compile was killed by user
-					if error?.terminated
+					if error?.terminated or error?.validate
 						OutputFileFinder.findOutputFiles request.resources, compileDir, (err, outputFiles) ->
 							return callback(err) if err?
 							callback(error, outputFiles) # return output files so user can check logs

+ 3 - 3
services/clsi/app/coffee/LatexRunner.coffee

@@ -8,11 +8,11 @@ ProcessTable = {}  # table of currently running jobs (pids or docker container n
 
 module.exports = LatexRunner =
 	runLatex: (project_id, options, callback = (error) ->) ->
-		{directory, mainFile, compiler, timeout, image} = options
+		{directory, mainFile, compiler, timeout, image, environment} = options
 		compiler ||= "pdflatex"
 		timeout  ||= 60000 # milliseconds
 
-		logger.log directory: directory, compiler: compiler, timeout: timeout, mainFile: mainFile, "starting compile"
+		logger.log directory: directory, compiler: compiler, timeout: timeout, mainFile: mainFile, environment: environment, "starting compile"
 
 		# We want to run latexmk on the tex file which we will automatically
 		# generate from the Rtex/Rmd/md file.
@@ -34,7 +34,7 @@ module.exports = LatexRunner =
 
 		id = "#{project_id}" # record running project under this id
 
-		ProcessTable[id] = CommandRunner.run project_id, command, directory, image, timeout, (error, output) ->
+		ProcessTable[id] = CommandRunner.run project_id, command, directory, image, timeout, environment, (error, output) ->
 			delete ProcessTable[id]
 			return callback(error) if error?
 			runs = output?.stderr?.match(/^Run number \d+ of .*latex/mg)?.length or 0

+ 3 - 0
services/clsi/app/coffee/RequestParser.coffee

@@ -28,6 +28,9 @@ module.exports = RequestParser =
 				compile.options.draft,
 				default: false,
 				type: "boolean"
+			response.check = @_parseAttribute "check",
+				compile.options.check,
+				type: "string"
 
 			if response.timeout > RequestParser.MAX_TIMEOUT
 				response.timeout = RequestParser.MAX_TIMEOUT

+ 2 - 0
services/clsi/test/unit/coffee/CompileManagerTests.coffee

@@ -47,6 +47,7 @@ describe "CompileManager", ->
 				compiler: @compiler = "pdflatex"
 				timeout: @timeout = 42000
 				imageName: @image = "example.com/image"
+			@env = {}
 			@Settings.compileDir = "compiles"
 			@compileDir = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
 			@ResourceWriter.syncResourcesToDisk = sinon.stub().callsArg(3)
@@ -72,6 +73,7 @@ describe "CompileManager", ->
 						compiler:  @compiler
 						timeout:   @timeout
 						image:     @image
+						environment: @env
 					})
 					.should.equal true
 

+ 4 - 2
services/clsi/test/unit/coffee/LatexRunnerTests.coffee

@@ -22,10 +22,11 @@ describe "LatexRunner", ->
 		@image     = "example.com/image"
 		@callback  = sinon.stub()
 		@project_id = "project-id-123"
+		@env       = {'foo': '123'}
 
 	describe "runLatex", ->
 		beforeEach ->
-			@CommandRunner.run = sinon.stub().callsArg(5)
+			@CommandRunner.run = sinon.stub().callsArg(6)
 
 		describe "normally", ->
 			beforeEach ->
@@ -35,11 +36,12 @@ describe "LatexRunner", ->
 					compiler:  @compiler
 					timeout:   @timeout = 42000
 					image:     @image
+					environment: @env
 					@callback
 
 			it "should run the latex command", ->
 				@CommandRunner.run
-					.calledWith(@project_id, sinon.match.any, @directory, @image, @timeout)
+					.calledWith(@project_id, sinon.match.any, @directory, @image, @timeout, @env)
 					.should.equal true
 
 		describe "with an .Rtex main file", ->